Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I implement a robust session-per-request pattern in my project, while focusing on information hiding?
    primarykey
    data
    text
    <p>I'm currently building an ASP.NET MVC project, with NHibernate as its persistance layer.</p> <p>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.</p> <p>The problem is that I want to leverage NHibernate's lazy-loading capabilities to improve the performance of my project.</p> <p>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).</p> <p>How can I achieve that as cleanly as possible?</p> <p>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.</p> <p>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.</p> <p>I have a few options in mind. Which one seems the best?</p> <ul> <li>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?</li> <li>Use the CurrentSessionContext Singleton provided by NHibernate in a Web context. Using <a href="http://hackingon.net/post/NHibernate-Session-Per-Request-with-ASPNET-MVC.aspx" rel="noreferrer">this page</a> as an example, I think this is quite promising, but that still requires filters at the controller level.</li> <li>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).</li> </ul> <p>Thank you very much!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload