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.
Need
sPersistenceAttribute
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); } }
NeedsPersistence
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);
}
}
DataAccessLayer.GetBooks()
method to use the following code:var session = MvcApplication.SessionFactory .GetCurrentSession(); var books = session.QueryOver<Book>() .List(); return books;
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.
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.
18.118.149.19