Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefit of using Unit of Work and Repository Patterns with Entity Framework

According to MSDN, DbContext is defined as:

Represents a combination of the Unit-Of-Work and Repository patterns and enables you to query a database and group together changes that will then be written back to the store as a unit.

Since DbContext implements the Unit of Work and Repository patterns, then why does this ASP.NET tutorial and other resources that I have found on the Internet demonstrate the use of DbContext with custom implementations of the Unit of Work and Repository patterns? Isn't this redundant?

If not, what is the benefit of creating custom implementations of the Unit of Work and Repository layers when using DbContext? (I can see how this might make sense within a Test project.)

like image 310
Usman Khalid Avatar asked Jun 30 '13 12:06

Usman Khalid


People also ask

What is the benefit of repository pattern in Entity Framework?

Benefits of Repository PatternIt centralizes data logic or business logic and service logic. It gives a substitution point for the unit tests. Provides a flexible architecture. If you want to modify the data access logic or business access logic, you don't need to change the repository logic.

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.

Do we need unit of work with Entity Framework?

No need for repositories and unit of work with Entity Framework Core.

What is unit of work pattern in Entity Framework?

The Unit of Work pattern is used to group one or more operations (usually database CRUD operations) into a single transaction or “unit of work” so that all operations either pass or fail as one unit.


1 Answers

Yes, DbContext represents a Unit of Work and DbSet represents a Repository, but some people will create a layer of abstraction over them. Here are some reasons people might do so:

  • Maybe they don't want their project tightly coupled to Entity Framework and its architecture. So, they hide Entity Framework behind those abstractions so they can substitute Entity Framework for any other ORM without any modification to the interface of the data access layer.
  • They use repositories to make it clear which operations are allowed for certain entities. (For example, CustomerRepository might allow adding and updating customers but not deleting them). On the other hand, it enables a client developer to easily recognize available operations for certain entities. In other words, they create repositories with naming conventions and interfaces that are compatible with the domain language.
  • Moving database-related operations to repositories allows you to intercept those operations and perform logging, performance tuning or any other operation you want.
  • Some do it to make testing easier. Say I have an ICustomerRepository interface with three methods. Then I can easily mock that up instead of mocking an IDbSet<Customer> with too many methods.
  • Finally, there are many who do not create an abstraction over DbContext and DbSet. They just use them directly and it is perfectly valid to do so.
like image 74
Ibrahim Najjar Avatar answered Oct 08 '22 09:10

Ibrahim Najjar