C
C#

help

❔ is Generic Repository a bad implementation of Repository Pattern?

Yyered.2/3/2023
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?
AAngius2/3/2023
Entity Framework is a generic repository
Jjcotton422/3/2023
is this on top of Entity Framework?
AAngius2/3/2023
So, yeah, it makes no sense to abstract a repository with a repository
Jjcotton422/3/2023
or on top of something like ADO?
AAngius2/3/2023
Outside of EF, tho, it does make sense
Yyered.2/3/2023
I forgot to say that I'm using EF, but thanks for yours replies
Jjcotton422/3/2023
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
Yyered.2/3/2023
Ok, thanks
QQu-nii-sama2/3/2023
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.
Jjcotton422/3/2023
are they talking generic repos or non-generic ones?
AAngius2/3/2023
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
Jjcotton422/3/2023
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
QQu-nii-sama2/3/2023
Im not sure i quite understand generic repos? is that just BaseRepository with generic methods
Jjcotton422/3/2023
it's like what @Angius just posted identical behavior across all types in the database
QQu-nii-sama2/3/2023
i see then yes identitcal behavior across all entities
AAngius2/3/2023
Then it's useless
QQu-nii-sama2/3/2023
you need to have that BaseRepository otherwise you will end up writing the same code for all repositories
AAngius2/3/2023
DbSet already has .Find(), why wrap it with another .Find()?
QQu-nii-sama2/3/2023
because it returns iqueryable and a repository pattern should not return iqeryable
AAngius2/3/2023
uh No it doesn't
Jjcotton422/3/2023
GitHub
spear/PromptService.cs at main · jcotton42/spear
Contribute to jcotton42/spear development by creating an account on GitHub.
AAngius2/3/2023
.Find() returns the given item
QQu-nii-sama2/3/2023
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
AAngius2/3/2023
Sure
Jjcotton422/3/2023
it has useful, specific, behaviors like authz checks, caching, etc.
AAngius2/3/2023
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?"
QQu-nii-sama2/3/2023
One of the arguments that i think is valid is that if you want to use multiple ORM technologies
Jjcotton422/3/2023
at the same time?
QQu-nii-sama2/3/2023
yes
Jjcotton422/3/2023
against the same database and entities? I can't think of why you'd want to do that
AAngius2/3/2023
Ig some people use EF for insertions and updates, and Dapper for querying
QQu-nii-sama2/3/2023
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
AAngius2/3/2023
That's easily rebuked by "YAGNI"
Jjcotton422/3/2023
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
AAngius2/3/2023
Just use Postgres and have the best of both worlds when
QQu-nii-sama2/3/2023
I think we should stick to the topic 🙂
AAngius2/3/2023
Sure
QQu-nii-sama2/3/2023
" 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
AAngius2/3/2023
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
AAccord2/4/2023
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Problems with using Dotnet 6 on Windows to publish to an exe formatDotnet/C# newb here. Previously I've only done a small amount of development using the **.Net Framew❔ Using net7 documentation while targeting netstandard2.0This might be a bit of a dumb question, but is there anyway to make omnisharp/roslyn's tooltips use ❔ WPF app unit testingI am learning how to make unit tests. I built my app on .net core 6.0 and I created class library (o❔ where exactly should i put authorizationso i read a few posts on where to put authorization and most of the people agree on putting it in th❔ C# WPF playing background musicHello, I have WPF app for game launcher/updater. I wanted to add background music, so I added this cC# wpf mvvm Datagrid filter using LINQSo here I have a MVVM form. the Form contains a Datagrid which is connected to the Databank. I also ❔ Is it worth to create lazy dictionary in this case?I have following class: so as you see Add function add to the dictionary class TreeNode, it looks li❔ Going to use a listbox with a switch for something in the morning, need help with format for bothI would like to set two variables with a listbox using a switch and one variable with a second listb✅ What's your view on tool like Sonarqube?Should I learn from the code smells it captures?✅ Autodesk Maya HelpAnyone in here know Autodesk Maya really well? My mesh tool isn't letting my polygon primitives mesh❔ circular primesso this program is meant to find how many circular primes there are below 1 million (should be 55), ❔ WPF Cannot find child when it's thereHi! I am working on my own RoundedCorners attachedproperty however I have stumbled upon a problem Pr❔ HttpClient exiting program during GetStringAsync() and GetAsync()I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out ✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray() ❔ force a ushort value?if i type for example var num = 25L; i am forcing a long value (int64) how can i force a ushort in ❔ Doing post method with entity frameworki'm making a simple API to practice with entity framework but when i try to make a post to a table w❔ I can't reference a class library please helpI can't reference a class library i t says compilation error❔ ASP.NET REMOTE DEVELOPMENTHey does anyone know if I would need nginx for remote development on a ubuntu server from my windowException/NULL/Error handling advice.DeserializeAsync clearly returns a nullable generic object. From the picture we can clearly see that❔ ASP.NET Core web app vs ASP.net web application(.NET Framework)Hi, Ive been working with c# for a while now and would like to make a webapp but I dont know where t