Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Unit of work in MVC: Responsibility

Who has the responsability


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

like image 697
SDReyes Avatar asked Feb 10 '10 16:02

SDReyes


People also ask

What is the unit of work responsible for when used with Entity Framework?

The Unit of Work holds all the entities involved in a Business logic into a single transaction and either commits the transaction or rolls it back.

What is unit of work in MVC?

The unit of work class coordinates the work of multiple repositories by creating a single database context class shared by all of them. If you wanted to be able to perform automated unit testing, you'd create and use interfaces for these classes in the same way you did for the Student repository.

What is relationship between Repository and unit of work?

Unit of Work is the concept related to the effective implementation of the repository pattern. non-generic repository pattern, generic repository pattern. Unit of Work is referred to as a single transaction that involves multiple operations of insert/update/delete and so on.

Is unit of work necessary?

The only reason to still have a unit of work is if you: want to include non-EF-datasources in an atomic data operation. want to use a unit of work in your domain without relying on an EF dependency on that layer.


1 Answers

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.

like image 162
zihotki Avatar answered Sep 19 '22 01:09

zihotki