Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

NHibernate sessions in ASP.NET MVC

0.00/5 (No votes)
17 May 2010 1  
NHibernate sessions in ASP.NET MVC

If you've used NHibernate in a ASP.NET MVC project, you probably encountered the problem of keeping session open in the view in order to retrieve model data (even though if session is open then the view actually makes calls to the db under the hood, which maybe is not very MVC. But that's another story).

The idea is to pass controllers an instance of a NHibernate session which is unique for the HTTP request.

First off, we need to "hijack" the ASP.NET mechanism that instantiates controllers to pass something in the constructor. In this case, it's a ISession, but it could well be a repository class (which in turn gets a ISession passed in its constructor).

So a controller would look like this:

C#
public class MyController : Controller
{
    ISession session;

    public MyController(ISession session)
    {
        this.session = session;
    }
...

I've used Unity dependency container to manage the instantiation of controllers. ASP.NET MVC lets you do that by overriding the DefaultControllerFactory.

This is as simple as writing this class:

C#
public class UnityControllerFactory : DefaultControllerFactory
{
    UnityContainer container;

    public UnityControllerFactory(UnityContainer container)
    {
        this.container = container;
    }

    protected override IController GetControllerInstance
	(RequestContext requestContext, Type controllerType)
    {
        IController controller = null;
        if (controllerType != null)
        {
            controller = this.container.Resolve(controllerType) as IController;
        }
        return controller;
    }
}

To make the session instance "singleton" for the HTTP request, all I need to do is write a simple custom lifetime manager for Unity:

C#
public class PerRequestLifetimeManager : LifetimeManager
{
    public const string Key = "SingletonPerRequest";

    public override object GetValue()
    {
        return HttpContext.Current.Items[Key];
    }

    public override void SetValue(object newValue)
    {
        HttpContext.Current.Items[Key] = newValue;
    }

    public override void RemoveValue() {} // this does not appear to be used ...
}

Let's get everything together in Global.asax. We need to register ISession in Unity configuration. It should inject it using a factory method (OpenSession method of a ISessionFactory instance).

Then we need to tell MVC to use our Controller factory, passing the Unity container of our ASP.NET MVC application.

To close open sessions, we add some code in the Application_EndRequest.

Here's the code to put in our Global.asax file.

C#
protected void Application_Start()
{
    this.container = new UnityContainer();
    container.RegisterType<isession>(
        new PerRequestLifetimeManager(),
        new InjectionFactory(c => {
            ISessionFactory sessionFactory = 
	         NHConfigurator.CreateSessionFactory(); // NHConfigurator is my 
							// helper conf class
            return sessionFactory.OpenSession();
        }));

    ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory 
(this.container));

    AreaRegistration.RegisterAllAreas();

    RegisterRoutes(RouteTable.Routes);
}

protected void Application_EndRequest(object sender, EventArgs e)
{
    Object sessionObject = HttpContext.Current.Items[PerRequestLifeTimeManager.Key];
    if (sessionObject != null)
    {
        ISession currentSession = sessionObject as ISession;
        if (currentSession != null)
        {
            currentSession.Close();
        }
    }
} 

Hope this helps!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here