title: What is the best way to manage Transaction with Nhibernate Repository
body: <p>Actually i try to found the best way to manage Transaction with Nhibernate with Repository pattern in MVC 5 context</p>
  
  <p>You can found my sample project here: <a href="https://github.com/Nono31/Pixel.Sample" rel="nofollow">https://github.com/Nono31/Pixel.Sample</a>
  My repository called by manager
  And my manager was called by controller</p>
  
  <p>Actually all works fine, but when I start NHProfiler, I have a warning "Use of implicit transactions is discouraged"
  (<a href="http://www.hibernatingrhinos.com/products/nhprof/learn/alert/donotuseimplicittransactions" rel="nofollow">http://www.hibernatingrhinos.com/products/nhprof/learn/alert/donotuseimplicittransactions</a>)</p>
  
  <p>My question is how to avoid implicit Transaction in my context ?
  In wich layer manage the transaction ?
  If I manage my transaction on Repository layer, the lazy loading entity are called outside the transaction.
  I've seen one solution with ActionFilterAttribute but there are any other solution ?</p>
  
  <pre><code>public class UnitOfWorkAction : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext context)
      {
          base.OnActionExecuting(context);
  
          if (!context.IsChildAction)
          {
              var session = DependencyResolver.Current.GetService&lt;ISession&gt;();
              session.BeginTransaction();
          }
      }
  
      public override void OnResultExecuted(ResultExecutedContext context)
      {
          base.OnResultExecuted(context);
  
          if (context.IsChildAction) return;
  
          using (var session = DependencyResolver.Current.GetService&lt;ISession&gt;())
          {
              if (session.Transaction != null &amp;&amp; session.Transaction.IsActive)
                  using (var transaction = session.Transaction)
                  {
                      try
                      {
                          var thereWereNoExceptions = context.Exception == null || context.ExceptionHandled;
                          if (context.Controller.ViewData.ModelState.IsValid &amp;&amp; thereWereNoExceptions)
                              transaction.Commit();
                          else
                              transaction.Rollback();
                      }
                      catch
                      {
                          transaction.Rollback();
                          throw;
                      }
                      finally
                      {
                          session.Close();
                      }
                  }
          }
      }
  }
  </code></pre>