2

Is it correct to create Unit of Work in order to share the DbContext among the Repositories?

If isn't what is the recommendation? I really think it is needed to share the DbContext sometimes.

I'm asking this because of the answer for this question: In-memory database doesn't save data

Andre
  • 652
  • 2
  • 7
  • 23

2 Answers2

0

What you need is a class that contains multiple repositories and creates a UoW. Then, when you have a use case in which you need to use multiple repositories with shared UoW, this class creates it and pass it to repositories.

I typically call this class Service, but I think there is not some standardized naming.

MacakM
  • 1,804
  • 3
  • 23
  • 46
0

Is it correct to create Unit of Work in order to share the DbContext among the Repositories?

It is design decision, but yes. There is no problem in doing that. It is absolutely valid that code from multiple repositories is executed under one single connection.

I really think it is needed to share the DbContext sometimes.

Absolutely; there are many times when you need to share DbContext.

Your linked answer is really good. I specially like the three points it mention. OP on that question is doing some unnecessary complicated things like Singleton, Service Locator and Async calls without understanding how they work. All these are good things but only if they are used at right time at right place.

Following is from your linked answer:

The best thing is that all of these could be avoided if people stopped attempting to create a Unit of Work + Repository pattern over yet another Unit of Work and Repository. Entity Framework Core already implements these:

Yes; this is true. But even so, repository and UoW may be helpful in some cases. This is design decision based on business needs. I have explained this in my answers below:

https://stackoverflow.com/a/49850950/5779732
https://stackoverflow.com/a/50877329/5779732

Using ORM directly in calling code has following issues:

  1. It makes code little more complicated.
  2. Database code is merged in business logic.
  3. As many ORM objects are used in-line in calling code, it is very hard to unit test the code.

All those issues could be overcome by creating Concrete Repositories in Data Access Layer. DAL should expose concrete repositories to calling code (BLL or Services or Controller whatever) through interfaces. This way, your database and ORM code is fully consumed in DAL and you can easily unit-test calling code by mocking repositories. Refer this article explaining benefit of repository even with ORMs.

Apart from all above, one other issue generally discussed is "What if we decide to change ORM in future". This is entirely false in my personal understanding. It happens very rarely and in most cases, should not be considered while design.

I recommend avoid overthinking and over-design. Focus on your business objectives.

Refer this example code to understand how to inject UoW in repositories. The code sample is with Dapper. But overall design may still useful to you.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141