Created
January 4, 2013 11:55
-
-
Save ChrisMcKee/4452072 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Web.Mvc; | |
| [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | |
| public class TransactionAttribute : ActionFilterAttribute | |
| { | |
| public override void OnActionExecuting(ActionExecutingContext filterContext) | |
| { | |
| var session = MvcApplication.SessionFactory.GetCurrentSession(); | |
| session.BeginTransaction(); | |
| } | |
| public override void OnActionExecuted(ActionExecutedContext filterContext) | |
| { | |
| var session = MvcApplication.SessionFactory.GetCurrentSession(); | |
| using (session.Transaction) | |
| { | |
| if (filterContext.Exception == null) | |
| { | |
| session.Transaction.Commit(); | |
| } | |
| else | |
| { | |
| session.Transaction.Rollback(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment