How to Implement a Unit of Work in MVC: Responsibility

Who is responsible


Who is responsible for starting and completing the Unit of Work in the MVC architecture?

+7
design-patterns architecture asp.net-mvc unit-of-work
source share
4 answers

This is not the responsibility of the controller, it violates the SRP . The controller does not need to know about UoW at all. A network typically uses one UoW for each server request. In this case, UoW should be located at the end of the request and start somewhere after the start of the request (ideally, the beginning of UoW should be lazy). The best place to do this is Global.asax (or your HttpApplication class) using the Application_EndRequest and Application_BeginRequest handlers.
This can be easily achieved using the IOC infrastructure (my favorite Windsor), see this question for implementation details.

+10
source share

Controller. This gets context, so you can start and finish work. For example, for an nHibernate session for each request, you need to know when the request was launched and completed, so you need a context to give you the request.

+5
source share

I am a supporter of loosely coupled architecture. My controller knows NOTHING about the repository, context, or unitofwork. I created a service level (not sure if this is the right term) that calls the controller. This service then works with the repository (dll) to save all the data.

+3
source share

As zihotki said, you will violate SRP if you transfer this responsibility to the controller. This is a data-oriented template manipulation and, as such, should not be a problem for the controller ... which would make it two violations: one for SRP and anothrt for SoC principle.

As for whoever is responsible, this is what is determined by your architecture. The StartRequest / EndRequest clause seems solid enough.

+2
source share

All Articles