#region Apache Notice
/*****************************************************************************
* $Header: $
* $Revision: 378715 $
* $Date$
*
* iBATIS.NET Data Mapper
* Copyright (C) 2006 - Apache Fondation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
********************************************************************************/
#endregion
using System.Web;
using IBatisNet.Common.Exceptions;
namespace IBatisNet.DataMapper.SessionStore
{
///
/// Provides an implementation of
/// which relies on HttpContext. Suitable for web projects.
/// This implementation will get the current session from the current
/// request.
///
public class WebSessionStore : AbstractSessionStore
{
///
/// Initializes a new instance of the class.
///
/// The SQL mapper id.
public WebSessionStore(string sqlMapperId) : base(sqlMapperId)
{}
///
/// Get the local session
///
public override ISqlMapSession LocalSession
{
get
{
HttpContext currentContext = ObtainSessionContext();
return currentContext.Items[sessionName] as SqlMapSession;
}
}
///
/// Store the specified session.
///
/// The session to store
public override void Store(ISqlMapSession session)
{
HttpContext currentContext = ObtainSessionContext();
currentContext.Items[sessionName] = session;
}
///
/// Remove the local session.
///
public override void Dispose()
{
HttpContext currentContext = ObtainSessionContext();
currentContext.Items.Remove(sessionName);
}
private static HttpContext ObtainSessionContext()
{
HttpContext currentContext = HttpContext.Current;
if (currentContext == null)
{
throw new IBatisNetException("WebSessionStore: Could not obtain reference to HttpContext");
}
return currentContext;
}
}
}