title: How to handle NHibernate sessions in an MVC4 project
body: <p>I currently get my session from the globalasax as follows...</p>
  
  <pre><code>public class MvcApplication : HttpApplication
  {
      public static readonly ISessionFactory SessionFactory =                                        NHibernateHelper.CreateSessionFactory();
  
      public MvcApplication()
      {
          BeginRequest += delegate
                              {
                                  if (!HttpContext.Current.Request.Url.AbsolutePath.StartsWith("/_cassette/"))
                                  {
                                      CurrentSession = SessionFactory.OpenSession();
                                      CurrentSession.FlushMode = FlushMode.Auto;
                                  }
                              };
          EndRequest += delegate
                          {
                              if (CurrentSession != null)
                              {
                                  CurrentSession.Flush();
                                  CurrentSession.Dispose();
                              }
                          };
      }
  
      public static ISession CurrentSession
      {
          get { return (ISession) HttpContext.Current.Items["current.session"]; }
          set { HttpContext.Current.Items["current.session"] = value; }
  </code></pre>
  
  <p>I was looking at the Sharp Architecture Transaction attribute and a similar one <a href="http://weblogs.asp.net/srkirkland/archive/2009/09/03/asp-net-mvc-transaction-attribute-using-nhibernate.aspx" rel="nofollow">http://weblogs.asp.net/srkirkland/archive/2009/09/03/asp-net-mvc-transaction-attribute-using-nhibernate.aspx</a> but whats the best way of handling sessions in an MVC4 project to make use of none-implicit transactions ala <a href="http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions" rel="nofollow">http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions</a></p>
  
  <p>I can easily wrap everything by adding the transaction/commit to the begin request/end request but the attribute method seems cleaner (actually handles errors); or should I be using a filter now?</p>
  
  <p>What is the best practice for MVC4 with NHibernate?</p>