The unit of work pattern within a asp.net mvc application

前端 未结 2 951
后悔当初
后悔当初 2021-02-02 04:08

I have been looking at this excellant blog titled \"NHibernate and the Unit of Work Pattern\" and have a question regarding the best place to use UnitOfWork.Start in a asp.net m

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-02 04:25

    I think Sharp Architecture solves this quite well. What they do is put the unit of work inside an ASP .Net MVC Action Filter. Basically you can define a transaction Action filter such as

    
    public class TransactionAttribute : ActionFilterAttribute
    {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
             UnitOfWork.Start();
          }
    
          public override void OnActionExecuted(ActionExecutedContext filterContext)
          {
             UnitOfWork.Stop();
          }
    }
    
    

    and in your controller class put the Transaction attribute on your Action Result method

提交回复
热议问题