C
C#2y ago
yered.

❔ is Generic Repository a bad implementation of Repository Pattern?

Surfing the internet I have found several articles that say it is an anti-pattern because it is an abstraction of an abstraction. In a personal project I implemented the generic repository to avoid repeating code that is normally used in CRUD and if I need more specific functions I create a new interface and implement it. Should I remove the generic repository and create a specific repository for each database table even if I need repeat the code of a CRUD?
40 Replies
Angius
Angius2y ago
Entity Framework is a generic repository
jcotton42
jcotton422y ago
is this on top of Entity Framework?
Angius
Angius2y ago
So, yeah, it makes no sense to abstract a repository with a repository
jcotton42
jcotton422y ago
or on top of something like ADO?
Angius
Angius2y ago
Outside of EF, tho, it does make sense
yered.
yered.2y ago
I forgot to say that I'm using EF, but thanks for yours replies
jcotton42
jcotton422y ago
yeah, in that case it's doing lots of harm and zero good the database context is already both a repository and a unit of work
yered.
yered.2y ago
Ok, thanks
Qu-nii-sama
Qu-nii-sama2y ago
As you pointed out you will find various debate on this topic, Ive been down that rabbit hole too, and been seaching the internet thin for answer According to Steve Smith - Ardalis Ms MVP and ASP, RP is not bad used with EF, Same with Mosh Hamedani , and I agree with them. Here are some source you can watch and read on https://www.youtube.com/watch?v=rtXpYpZdOzM&t=585s&ab_channel=ProgrammingwithMosh
https://www.weeklydevtips.com/episodes/024 And I ended up agreeing with Mosh And Steve. Here is mt perspective 1. RP is a design to implemented different ORMs or Data access technology, where EF is a ORM technology with that said I think we can conclude that its not the same. 2. Minimizes duplicate query logic, EF can return Iqueryable and you will with time have a lot of Iqueryable around your code because of incl. and thenIncl. and etc. Where RP can encapsulate those query and should 3. Decouples application from persistence framework 4. Testability ( look Ardalis article i linked ) And heres too why some may think its an anti pattern - "The repository pattern is a design pattern that provides a centralized approach to access data from a database or any other data source. It acts as an intermediary between the data source and the application, providing a unified API for accessing the data. While the repository pattern can provide many benefits, such as separation of concerns, decoupling of the data layer, and a clean API, it is considered by some as an antipattern in certain contexts. Some developers believe that the repository pattern can introduce unnecessary complexity and can lead to tight coupling between the application and the data layer. Additionally, over-reliance on the repository pattern can result in an anemic domain model, where the majority of the business logic is located in the data layer, rather than the domain layer. In summary, the repository pattern is not inherently an antipattern, but it can become one if it is used in an inappropriate manner or if it is applied blindly without considering the specific context. "
Programming with Mosh
YouTube
Repository Pattern with C# and Entity Framework, Done Right | Mosh
🔥Get the COMPLETE Entity Framework course (80% OFF - LIMITED TIME): http://bit.ly/2rZAgrD Want to learn more from me? Check out my these links: Courses: https://codewithmosh.com Blog: https://www.programmingwithmosh.com/ Facebook: https://www.facebook.com/programmingwithmosh Twitter: https://twitter.com/moshhamedani Confused about the reposito...
Simplecast
Do I Need a Repository? | Weekly Dev Tips
This week we'll answer this extremely common question about the Repository pattern, and when you should think about using it.
jcotton42
jcotton422y ago
are they talking generic repos or non-generic ones?
Angius
Angius2y ago
Because a generic repo does
List<T> GetAll<T>() => _ctx.Set<T>().ToList();
List<T> GetAll<T>() => _ctx.Set<T>().ToList();
which is nothing more than just a useless passthrough method
jcotton42
jcotton422y ago
if you have specific repos that add value, then those make sense like in my discord bot, I have a PromptService that's really a repo when you do things like AddPrompt it will check that the active user has permission to do that
Qu-nii-sama
Qu-nii-sama2y ago
Im not sure i quite understand generic repos? is that just BaseRepository with generic methods
jcotton42
jcotton422y ago
it's like what @Angius just posted identical behavior across all types in the database
Qu-nii-sama
Qu-nii-sama2y ago
i see then yes identitcal behavior across all entities
Angius
Angius2y ago
Then it's useless
Qu-nii-sama
Qu-nii-sama2y ago
you need to have that BaseRepository otherwise you will end up writing the same code for all repositories
Angius
Angius2y ago
DbSet already has .Find(), why wrap it with another .Find()?
Qu-nii-sama
Qu-nii-sama2y ago
because it returns iqueryable and a repository pattern should not return iqeryable
Angius
Angius2y ago
uh No it doesn't
jcotton42
jcotton422y ago
GitHub
spear/PromptService.cs at main · jcotton42/spear
Contribute to jcotton42/spear development by creating an account on GitHub.
Angius
Angius2y ago
.Find() returns the given item
Qu-nii-sama
Qu-nii-sama2y ago
aaah my bad 😄 i was a bit too fast on the keys i was stuck on incl from previous post i need to find all my old sources to come up with answers
Angius
Angius2y ago
Sure
jcotton42
jcotton422y ago
it has useful, specific, behaviors like authz checks, caching, etc.
Angius
Angius2y ago
But so far the only arguments in favour of generic repos over EF I heard are "abstraction good" and "what if I want to change the database, the provider, and the whole dev team one day?"
Qu-nii-sama
Qu-nii-sama2y ago
One of the arguments that i think is valid is that if you want to use multiple ORM technologies
jcotton42
jcotton422y ago
at the same time?
Qu-nii-sama
Qu-nii-sama2y ago
yes
jcotton42
jcotton422y ago
against the same database and entities? I can't think of why you'd want to do that
Angius
Angius2y ago
Ig some people use EF for insertions and updates, and Dapper for querying
Qu-nii-sama
Qu-nii-sama2y ago
if you suddenly need to use a nosql db or the otherway around i guess , i havent implemented it myself or as z porinted out
Angius
Angius2y ago
That's easily rebuked by "YAGNI"
jcotton42
jcotton422y ago
that's going to involve radical changes to your data structure you don't just switch from relational to non-relational without large changes they're completely different paradigms
Angius
Angius2y ago
Just use Postgres and have the best of both worlds when
Qu-nii-sama
Qu-nii-sama2y ago
I think we should stick to the topic 🙂
Angius
Angius2y ago
Sure
Qu-nii-sama
Qu-nii-sama2y ago
" Should I remove the generic repository and create a specific repository for each database table even if I need repeat the code of a CRUD?" The answer is NO because you will end up violating DRY principle
Angius
Angius2y ago
But, yeah, if you want to swap a database out, chances are you'll end up needing much more changes that swapping out what repo you use
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts