✅ Dependency Injection question
Should Repositories be Scoped or Transient? They are injected into MediatR request handlers
70 Replies
off the top of my head, I'd say scoped
but that really means nothing
the only REAL answer is "depends on context"
what do your repositories do, and how are they used?
mediatr handlers are transient, idk how it handles scoped services
accessing data from dbcontext using ef core, for example getcategorybyid
if they're pulling scoped services, they should maybe also be scoped
unless they maintain no state, and you don't expect them to be used more than once per scope
in which case transient prolly makes more sense
ty that's what I thought to use, some course on clean arhitecture I watched just uses scoped with no explanation why
mediatr handlers shouldn't be scoped, and that's not the default if you use its DI integration
also, is your EF context your repository or are you doing that bad thing where you're wrapping EF in repositories
there is nothing wrong with wrapping EF with a repository layer
the wrongess is in HOW you implement a repository layer
I just dependency inject it into repository implementation
and how do you use the repository in your handlers?
seems pretty similar to what i've been doing, except i don't use a repository or mapper
all that gets done in the handler, because that's what the handler is for
what's on
BaseRepository
i am trying to learn clean arhitecture
that's why there is a mapper
implementation of default interface for deleting updating entities
and
IAsyncRepository<>
yes, whatsec
that definitely smells like the repository anit-pattern
transient can use scoped
just, with the assumption that they shouldn't be used in root scope
thaaaaaaaaaaaaaaaaaaaaaaaaat is the repository anti-pattern
yeah that gives you nothing over just injecting a dbcontext
it already does all that
what does repository anti pattern mean
(also AddAsync on the DbSet is rarely useful)
you've just created a kneecapped version of DbSet
EF core already implements the repository pattern with dbsets
severely kneecapped
for basically no gain
the ONLY gain you've got here is that it's slightly more abstractable than a DbSet
slightly
i mean, if you're switching ORMs deep into a project you probably have other issues
so the slight abstraction is not worth it?
no
lemme go grab one of my handlers
you're throwing away all the power of EF
abstraction of a DbSet/DbContext isn't for the purpose of being able to change out your data layer, that's a fool's errand. It's pretty much exclusively for testing
(and I'd argue for testing you should use an actual database)
depends on the testing you want
so instead of repositories I should just directly access dbcontext?
that's all it has to be
anyway, yeah, there can be value in using a repository as a testing seam, or just as a mechanism for organization
i implemented paging/ordering/etc as extension methods on IQueryable
like, keeping data query logic in one place
since you're projecting I don't think that
AsNoTracking
is doing anythingiirc it gets upset if you try to do certain types of projection with a tracking query
unless you have a specific reason not to
don't just make a repository layer cause it's "clean" or whatever
for me, the big anti-pattern is having that
RepositoryBase<T>
that forces you into assumptions about how your entities worki need to clean up that project in general though, i botched the idea of VSA and didn't get any of the benefits
implement queries as you need them, don't assume all entites should be inserted/deleted/updated/etc. the exact same way
i wondered why the course creator just threw away iqueryable
and opted out for a load everything into list method
incompetence
to be brutally honest
that's a bit of an oversimplification, but.... yeah
i am gonna restructure the project to remove that abstraction
I mean, if your instructor is expecting it.... maybe not
no i am just following a course to learn splitting an api into multiple projects
oh, so this is like an online thing?
like a video series or blog series?
ugh
where is that from?
UGH
I thought pluralsight was supposed to be reputable
well, anyway
i found few holes
there IS something to be said about the "cleanness" of keeping data-access logic separate from business logic
but don't add an abstraction/cleanliness layer when you're not getting demonstrable benefit
and don't hamstring yourself into implementing all things a certain way, by making interfaces/base classes for everything
use those tools sparingly, when you have an appropriate reason
that's just as much a part of "Clean" architecture as anything else
before I just put all files into single project and it got messy after 1 month
don't over-implement
and clean architecture is a fine thing to learn to address that problem
the topic of repositories over EF, in particular, is contentious around here
I don't see myself switching from EF but if I wanted to switch to Dapper lets say, wouldn't the repositories help?
so there's two ways this would go
maybe
as in
not that having repositories would make the switch easier
more that repositories might fit a little better over top of Dapper
1) you constrain yourself to features common to both EF and Dapper, switching won't be that hard, but your dev experience will suck and you'll be underutilizing EF
2) you use EF to it's potential, and a switch to Dapper involves an absolute assload of work
Dapper isn't really itself a repository library, as I understand
ty for all info i will make use of it
o7
it is reputable
I'm sure there is a reason why the author is not implementing it following all best practices up front. The is something called "cognitive overload" and you wanna explain one concept at a time. The way these courses often work is they show how to get it working at all first, and then how to improve it step by step following best practices @ReactiveVeina