I am new to Repository pattern.
When I am managing the CRUD operations of several entities (like: customers, orders etc) then its fine. I am making an interface, I am making a Generic repository. And that serves my purpose, because CRUD operation is common for them.
My question is:
When the duties of several entities are totally different, there is no common method between them, in this case what should I do? Should I increase the number of interfaces and repositories for those specific purposes? Or is there any better solution in terms of best practices?
How should I manage Generic Repository Pattern when the works of different entities are pretty much different?
This is the core problem with Generic Repository pattern; that is why it is considered an anti-pattern.
I read this here:
No matter what clever mechanism I tried, I always ended up at the same problem: a repository is a part of the domain being modeled, and that domain is not generic. Not every entity can be deleted, not every entity can be added, not every entity has a repository. Queries vary wildly; the repository API becomes as unique as the entity itself.
Why generic repository is anti-pattern?
GetById(), identifier types may be different.I suggest you read these (1, 2, 3, 4, 5) articles explaining why generic repository is an anit-pattern.
Better approach is:
In any case, do not expose generic repository to calling code. Also, do not expose IQueryable from concrete repositories.
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