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?
Thank you very much!
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!
Use DI along with an IoC. Most IoC's come with a per-request instantiation behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With