Certain services could not be instantiated due to iss
Hi , I am trying implement Unit of Work design pattern. I am unable to locate the mistake here. Could someone please guide me here?
16 Replies
are you sure you always resolve this as
IDepartmentRepository
? do you have the actual error message?@Pobiega this is actual error mesage
Ah
you should probably not request a
DbContext
in your repository
it should be your actual repository
err
actual context
the services.AddDbContext<YourContextType>();
line adds your context to the DI
its THAT type you need to request Already added that line and removed dbcontext from the repo. But same error.
wdym
cant be
show new
DepartmentRepository
sourcelook at your constructor
still requesting a
DbContext
and not ApplicationDbContext
ok
this is my base class
is the base class has any problem?
no, thats fine now.
I feel I should mention that this is a bit redundant thou
EF already implements Unit of Work AND repository patterns
so you are just wrapping a UoW in another UoW
little bit lost here. could you please elaborate more?
i do not need base class repo here?
Well, look at what methods are available on
DbSet<T>
and see what your code does
you are literally just calling an existing method, then calling save changesmake sense
DbSet<T>
is a generic repository over T already
and guess what ApplicationDbContext
is?
its your unit of work 🙂
So what I'm trying to say is, if you are just doing this to learn how UOW/repo works, go ahead
but please do not actually wrap EFCore with UoW/Repo in a real codebase, because its superfluent
UoW/repo makes a lot more sense if you are wrapping a raw database connection, or some other source, but not EF/EFCorethanks for the help. it makes a lot of sense.