Confusion about Repository Pattern and Unit of Work.
Hi there. I recently applied the Repository Pattern and Unit of Work in an API I'm creating, and everything was going well. However, I got stuck trying to understand how to pass the Database Context to the implementation of one of my interfaces. I'm trying to grasp it, but I can't figure it out. I'll attach few code for reference:
(Program.cs) Here is where I'll apply Dependency Injection for my ICategoryRepository with a Scoped lifetime:
(DataAccess/Repository/Repository.cs) Here is my implementation of my Repository Interface (only constructor and fields):
(DataAccess/Repository/CategoryRepository.cs) And here is my implementation of my Category Repository Interface (only constructor, as always):
Look how these two has inyected with the DbContext, but here comes my confusion...
In the controller, I created an ICategoryRepository object, but I don't understand how it pass the DbContext to the controller itself...
11 Replies
The controller doesn't need the dbcontext
The context gets injected into the repository, the repository gets injected into the controller
As long as the dbcontext and the repository are registered in the DI container, it'll just work
What's the DI? And thanks for the answer!
Dependency Injection
The
builder.Services.AddSomething<I, T>
Oh, ok. Now I understand. Let me know something about the Unit of Work: how does it exactly works? I did this implementation:
Just passed the context and generated a new CategoryRepository object with the context as well.
Also implemented a Save() method (to save changes to the db)
Honestly, dunno
I never used generic repository pattern, because why would I ever
DbContext already implements UoW, so I never saw much point
It is supposed to have certain benefits as well as its drawbacks, but I didn't know that DbContext already comes with UnitOfWork included...
IME generic repositories are all drawbacks and no benefits
But some companies do require it in their codebase, so knowing what it is and how it works is useful ig
Well, I think I'll continue learning these kinds of things in a general sense, at least to understand them... even if I don't end up using them.
Thank you very much for the response, really ^^
i don't think you can really use repository and unit of work together...? with unit of work you would perform the entire "operation", which could theoretically span multiple dbsets
so i guess you could internally use every dbset as a repository, but it would be useless
I mainly do it to have cleaner code. Because with the Repository Pattern, even though it has the advantage of decoupling components, in the long run, I'll end up adding about 10 different repositories and having to apply each of them individually.
On the other hand, with UnitOfWork, at least I can implement them all within itself and work with it. I know it has the disadvantage that, in the long run, one controller will have things it shouldn't... but at least the advantage is that the code feels cleaner. I'm not sure if this will cause me problems in the future, though...
dbcontext is already decoupling logic and db implementation; also, even if maybe is not decoupled as much as you want, would you really use/benefit from that level of isolation?
I'm not sure if this will cause me problemsthere will always be "problems" to solve if the software continues to evolve, i believe the main decision would be the complexity/size of the software, or chosing the right amount of decoupling based on how much really you would need it with a repository pattern (and no unit of work) you could implement the data layer for a small application (if you say you have only 10 repositories), it wouldn't be as elegant and as easy to use as mediatr, but it could have its place on the other hand it depends also how you use those repositories