11

I'm currently building an ASP.NET MVC project, with NHibernate as its persistance layer.

For now, some functionality have been implemented, but only use local NHibernate sessions: each method that accessed the database (read or write) needs to instantiate its own NHibernate session, with the "using()" clause.

The problem is that I want to leverage NHibernate's lazy-loading capabilities to improve the performance of my project.

This implies an open NHibernate session per request until the view is rendered. Furthermore, simultaneous requests must be supported (multiple Sessions at the same time).

How can I achieve that as cleanly as possible?

I searched the web a little bit and learned about the session-per-request pattern. Most of the implementations I saw used some sort of Http* (HttpContext, etc.) object to store the session. Also, using the Application_BeginRequest/Application_EndRequest functions is complicated, since they get fired for each HTTP request (aspx files, css files, js files, etc.), when I only want to instantiate a session once per request.

The concern that I have is that I don't want my views or controllers to have access to NHibernate sessions (or, more generally, NHibernate namespaces and code). That means that I do not want to handle sessions at the controller level nor the view one.

I have a few options in mind. Which one seems the best?

  • Use interceptors (like in GRAILS) that get triggered before and after the controller action. These would open and close sessions/transactions. Is this possible in the ASP.NET MVC world?
  • Use the CurrentSessionContext Singleton provided by NHibernate in a Web context. Using this page as an example, I think this is quite promising, but that still requires filters at the controller level.
  • Use the HttpContext.Current.Items to store the request session. This, coupled with a few lines of code in Global.asax.cs, can easily provide me with a session on the request level. However, it means that dependencies will be injected between NHibernate and my views (HttpContext).

Thank you very much!

sduplooy
  • 12,740
  • 9
  • 39
  • 59
Guillaume Gervais
  • 1,015
  • 2
  • 14
  • 26
  • "dependencies will be injected between NHibernate and my views" -> it's not quite clear what you mean with this, can you explain further? – Mauricio Scheffer Jan 12 '10 at 02:40
  • If I use HttpContext to store an NHibernate session, the View aspect of my MVC application will be coupled with the data access aspect (NHibernate session). – Guillaume Gervais Jan 12 '10 at 13:01
  • @ggervais: that only happens if you use lazy-loading in your views, which you shouldn't. – Mauricio Scheffer Jan 13 '10 at 19:35
  • @mauricio-scheffer: What should be my course of action, then? For example, if I need to display a number of submissions in HTML, I'll do something like User.Submissions.Count(), directly in my view. However, the Submissions collection might not have been loaded yet by NHibernate in the controller or model layer. Also, controllers and views are both instanciated on a per-request basis. Shouldn't the NHibernate session behave the same way? Finally, if I follow your advice, how can I make sure that my objet is fully loaded (only the needed properties) in the view, while still using lazy-loading? – Guillaume Gervais Jan 14 '10 at 00:59
  • I think there's been some misunderstanding... if you want your views to be **fully NHibernate-free** the only way is to eagerly fetch everything the view needs in your controllers/services, *before* passing the data to the view. Within the controllers/services you could use lazy-loading as you please. This has nothing to do with the NHibernate session being per-request (which it should be, and almost everyone does this with a HttpModule) – Mauricio Scheffer Jan 14 '10 at 03:04

5 Answers5

13

Well guys, after a few days' work, I finally decided to use the HttpContext.Current.Items to load the session.

It works great!

Here's how I did it

import System.Web
class SessionManager {
    public static ISession GetSession()
        var session = HttpContext.Current.Items["NHibernateSession"];
        if (session == null) {
            session = ...; // Create session, like SessionFactory.createSession()...
            HttpContext.Current.Items.Add("NHibernateSession", session);
        }
        return session;
    }

    public static void CloseSession()
    {
        var session = HttpContext.Current.Items["NHibernateSession"];
        if (session != null) {
            if (session.IsOpen) {
                session.close();
            }
            HttpContext.Current.Items.Remove("NHibernateSession");
        }
    }
}

By using the static methods provided by this class, one can get a session (for example, in a Controller) that is tied to the current HttpContext (the current Web request). We need another snippet of code to call the CloseSession() method when the request is completed.

In Global.asax.cs:

protected void Application_EndRequest(object sender, EventArgs args)
{
    NHibernateSessionManager.CloseSession();
}

The Application_EndRequest event is automatically called when the session is completed, so the session can be properly closed an disposed of. This is useful, because otherwise we would have to do this in every Controller!

Guillaume Gervais
  • 1,015
  • 2
  • 14
  • 26
  • 1
    Can you give more detail please for those of us trying to figure out how to set this up? – Joe Phillips Sep 08 '10 at 18:29
  • I think this might create problems while you do unit testing, need to take care of the same. I came across this link http://www.prideparrot.com/blog/archive/2012/7/how_to_create_a_custom_session_value_provider which might be good option to go with. Any inputs would be good? – Siva Karthikeyan Feb 08 '13 at 03:41
2

Use DI along with an IoC. Most IoC's come with a per-request instantiation behaviour.

Charlino
  • 15,532
  • 3
  • 54
  • 72
2

My "solution" involves using Unity to inject session per request in controllers:

http://letsfollowtheyellowbrickroad.blogspot.com/2010/05/nhibernate-sessions-in-aspnet-mvc.html

Giorgio Bozio
  • 2,942
  • 2
  • 17
  • 20
1

Take a look at S#arp Architecture. It is a very nice architecture for ASP.NET MVC and NHibernate.

Mark Sherretta
  • 9,930
  • 4
  • 35
  • 42
1

You can add an action filter that can manage your NHibernate session & transactions. (This can be done at the action or controller level.) Here is an example of it:

http://weblogs.asp.net/srkirkland/archive/2009/09/03/asp-net-mvc-transaction-attribute-using-nhibernate.aspx

Josh
  • 961
  • 8
  • 18