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?
7 Replies
I couldn't fit the code and working with images is hard, so sorry in advance
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
you mean the generic dbsets that i can access?
if yes can you send me any links
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
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
i see
@𝗠 𝗕 𝗔 𝗥 𝗞 @Jimmacle thanks guys ill search up some more then
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