C
C#13mo ago
daysleeper

Generic access to DbContext implementation

Okay, so I wanted to do the generic repository for my project and my searches in google didn't give me satisfying answers so I tried to do something myself. My question is whether my attempt is right or wrong and why? (.NET 6, EF 6) My entities all inherit from Entity class. So first thing that bothers me is thread safety, is it thread safe, am I using DbContext.Set() method right? Also the optimization part, would invoking Set() for every Repository like I did be much resource heavier?
No description
No description
No description
No description
No description
No description
7 Replies
daysleeper
daysleeperOP13mo ago
I couldn't fit the code and working with images is hard, so sorry in advance
Jimmacle
Jimmacle13mo ago
it is wrong because an EF dbcontext already implements generic repositories and unit of work you don't gain anything by adding another layer of abstraction on top of it
daysleeper
daysleeperOP13mo ago
you mean the generic dbsets that i can access? if yes can you send me any links
Jimmacle
Jimmacle13mo ago
correct, DbSet<T> is a generic repository in itself and no, DbContexts are not thread safe - you should pretty much instantiate a new one for each transaction
M B V R K
M B V R K13mo ago
As it known using Repository pattern with EF Core is wrong, because EF Core is already implemented that pattern, but if you have some very special cases that could be handled better with Rpository pattern its okay or if you implementing DDD you will need Repositories over the EF Core
daysleeper
daysleeperOP13mo ago
i see @𝗠 𝗕 𝗔 𝗥 𝗞 @Jimmacle thanks guys ill search up some more then
M B V R K
M B V R K13mo ago
If you don't have any special cases or you don't implementing DDD you don't need Repository, as an unsatisfying solution you cann use Extension Methods to extensd the DbSet<T> by your own methods, but still not a solution that fiit all cases
Want results from more Discord servers?
Add your server