Does UnitOfWork template set value in EF application?

EntityFramework allows me to create tables from POCO classes. Repository pattern allows me to create an abstraction layer on top of database concepts so that I can focus on programming the actual application.

But then, the UnitOfWork (UoW) template. This is a template that is widely advertised on the Internet, especially in combination with EF.

I use several repositories and I have the need to do things in Atomic. Then I use each UoW example, I see that they implement the SaveChanges method, which does nothing, and then passes the call to DbContext.SaveChanges . It seems a little subtle, so I will probably skip the point. Or is this UnitOfWork nothing more than a DbContext wrapper?

Example script:

I have a MultiTenant web application. When a new Tenant is created, the user who created Tenant must also be saved as the user for the newly created Tenant . The class that is responsible for creating the new Tenant and assigns the first User in my current case is the Tenant class itself. If something fails, neither Tenant nor User should be stored in the database.

Now I am processing this, since DbContext.SaveChanges not called before all actions have been completed successfully.

Can someone (preferring an expert in this domain who actually uses UoW in real EF solutions) explain the advantage of using UnitOfWork relying on DbContext ?

Can an expert tell me about naming conventions? TenantAndUserUnitOfWork strikes me so long and free to interpret (not what you expect in an object oriented world, or am I wrong?)

+4
source share
1 answer

Does the UnitOfWork template add value in an EF application?

A unit of work is a good way to represent a domain transaction. Thus, does it add value to the application using it (either with the Entity Framework, or with any other approach to the repository)?

Yes: good software should perform atomic operations in some scenarios .

  • I need to register a user and create a profile for him.
  • I need to delete the order and notice that the user has been deleted.
  • ...

Everything can be part of a unit of work.

Answer: yes, if you want to create serious software.

+3
source

All Articles