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).
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With