This attribute will be applied to any action method within an MVC controller, or even to an entire MVC controller class. It will cause the entire action (or any action in the controller, depending on usage) to be executed inside of a transaction, properly committing the transaction on successful method completion. If any unhandled exception occurred, the transaction will be rolled back.
To get started we are going to create an attribute that derives from ActionFilterAttribute, which is in the System.Web.Mvc namespace. By doing this you can override any of several methods to hook into the lifecycle of your controller method.
The basic idea will be to begin the transaction before the action is executed and then commit the transaction after the action have finished executing (or rollback if there were any exceptions).
Here is the complete implementation of the TransactionAttribute class:
Here the OnActionExecuting method gives us a place to begin the transaction, and the OnActionExecuted method is called after the controller action has been executed (and helpfully provides us with any exception through the filterContext.Exception property).
With NHibernate Profiler (which I highly recommend to anyone working with NHibernate), you can see that a transaction was started and committed around the SQL generated from the ProjectRepository.GetAll() method. Now that is some efficient and good looking SQL.
[I know, this is pretty dead simple but I am trying to work into blogging again so I am starting with the easy stuff!]