Session Module is the heart & head of nhibernate framework, so here i am publishing the design of SessionModule
Reference required :
using System;
using System.Web;
using NHibernate;
using NHibernate.Cfg;
Session Module class :
public class SessionModule : System.Web.IHttpModule
{
private static readonly string SESSION_KEY = "CONTEXT_SESSION";
private const string TRANSACTION_KEY = "CONTEXT_TRANSACTION";
[ThreadStatic]
private static NHibernate.ISession m_session;
private static ISessionFactory m_sessionFactory;
public void Dispose()
{
}
public void Init(System.Web.HttpApplication context)
{
log4net.Config.XmlConfigurator.Configure();
context.BeginRequest += new EventHandler(BeginRequestHandlerExecute);
context.EndRequest += new EventHandler(EndRequestHandlerExecute);
// http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx
Configuration cfg = new Configuration();
m_sessionFactory = cfg.BuildSessionFactory();
}
private void BeginRequestHandlerExecute(object sender, EventArgs e)
{
CurrentSession = m_sessionFactory.OpenSession() ;
}
private void EndRequestHandlerExecute(object sender, EventArgs e)
{
ISession currentSession = CurrentSession;
if (currentSession != null)
currentSession.Close();
}
public static ISession CurrentSession
{
get
{
ISession session = (ISession)HttpContext.Current.Items[SESSION_KEY];
return session;
}
set
{
HttpContext.Current.Items[SESSION_KEY] = value;
//SetCurrentSession(value);
}
}
public static ITransaction CurrentTransaction
{
get
{
ITransaction tran = (ITransaction)HttpContext.Current.Items[TRANSACTION_KEY];
return tran;
}
set
{
HttpContext.Current.Items[TRANSACTION_KEY] = value;
}
}
}
No comments:
Post a Comment