Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying Unit Of Work pattern

I have read in Patterns of Enterprise Application Architecture that a Unit Of Work should only be used in a single Session. So each session should have its only Unit Of Work. Can anybody tell me why I could not use one Unit Of Work for the whole application (in my case ASP.NET).

like image 908
Lieven Cardoen Avatar asked Oct 23 '08 13:10

Lieven Cardoen


People also ask

How do you use the unit of work pattern?

Unit of Work in the Repository Pattern This means, one unit of work here involves insert/update/delete operations, all in one single transaction. To understand this concept, consider the following implementation of the Repository Pattern using a non-generic repository, for a Customer entity.

Why do we use unit of work pattern?

The unit of work class serves one purpose: to make sure that when you use multiple repositories, they share a single database context. That way, when a unit of work is complete you can call the SaveChanges method on that instance of the context and be assured that all related changes will be coordinated.

What is Work Unit example?

Some commonly used work units also include erg in the CGS system, the horsepower-hour, the newton-metre, the foot-pound, the kilowatt-hour, the foot-poundal, and the litre-atmosphere.

Should I use unit of work?

Ideally to completely decouple the business logic layer from persistence layer, unit of work pattern can be used as it can easily coordinate the writing out of changes made to objects using repository pattern. So, saving changes is the responsibility of unit of work.


1 Answers

Half of the unit of work pattern is to keep track of changes in a transaction and you could certainly track that for an entire application (this seems to be pretty common) but the other half is the resolution of concurrency problems which becomes meaningless if you are applying the pattern to the entire application rather than on a per session level.
Also, at some point you have to decide "Hey this is a unit...time to commit" and that might be difficult when taking the whole application into account with different users doing different things at the same time.

like image 161
Ichorus Avatar answered Nov 11 '22 15:11

Ichorus