Creating a transaction ASP.NET MVC action filter

We can extend the concepts of the previous recipe to NHibernate transactions as well. In this recipe, we'll show you how to create an action filter to manage our NHibernate sessions and transactions in an MVC or Web API 2 application.

Getting ready

Complete the previous recipe, Creating a session ASP.NET MVC action filter.

How to do it…

  1. Add the NeedsPersistenceAttribute class:
    public class NeedsPersistenceAttribute
    : NHibernateSessionAttribute
    {
    
      protected ISession session
      {
        get
        {
          return sessionFactory.GetCurrentSession();
        }
      }
    
      public override void OnActionExecuting(
        ActionExecutingContext filterContext)
      {
        base.OnActionExecuting(filterContext);
        session.BeginTransaction();
      }
    
      public override void OnActionExecuted(
        ActionExecutedContext filterContext)
      {
    
        var tx = session.Transaction;
        if (tx != null && tx.IsActive)
        {
          var noUnhandledException = 
            filterContext.Exception == null || 
            filterContext.ExceptionHandled;
    
          if (noUnhandledException &&
          filterContext.Controller.ViewData.ModelState.IsValid)
          {
            session.Transaction.Commit();
          }
          else
          {
            session.Transaction.Rollback();
          }
        }
    
        base.OnActionExecuted(filterContext);
      }
    
    }
  2. Decorate your controller actions with theNeedsPersistence attribute as shown in the following lines of code:
    public class BooksController : Controller
    {
    [NeedsPersistence]
      public ActionResult Index()
      {
        var books = DataAccessLayer.GetBooks()
            .Select(x=>new BookModel {
                     Id=x.Id, 
              Name=x.Name,
              Author= x.Author});
        return View(books);
      }
    }
  3. Update the DataAccessLayer.GetBooks() method to use the following code:
    var session = MvcApplication.SessionFactory
      .GetCurrentSession();
    var books = session.QueryOver<Book>()
      .List();
    return books;

How it works…

Before ASP.NET MVC executes the controller action, our NeedsPersistence action filter starts a new session and NHibernate transaction. If everything goes as planned, as soon as the action is completed, the filter commits the transaction. However, if an exception is thrown or if some kind of model validation error is present, the transaction will be rolled back. Note that we no longer need to use a transaction in our data access layer, as the entire controller action is wrapped in a transaction.

There's more…

This attribute inherits from our session action filter defined in the previous recipe. If you're managing your session differently, such as session-per-request, inherit from ActionFilterAttribute instead.

Just as in the previous recipe, we can adapt this filter for use with ASP.NET Web API, just by deriving from a different base class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.175