How to implement Unit of work in MVC: Responsibility

前端 未结 4 1601
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 09:08

Who has the responsability


Who has the responsibility to start and finish the Unit of work in a MVC architecture?

4条回答
  •  再見小時候
    2021-01-12 09:45

    It's not a responsibility of a controller, it violates SRP. Controller should not even know about UoW at all. In web, one UoW per request to server is usually used. In this case UoW should be disposed at the end of a request and started somewhere after the beginning of a request (ideally start of a UoW should be lazy). The best place to do this is Global.asax (or your HttpApplication class) using Application_EndRequest and Application_BeginRequest handlers.
    This can be easily achieved with an IOC framework (my favorite is Windsor), see this question for implementation details.

提交回复
热议问题