✅ How to avoid creating same CRUD Commands/Queries for different entities | MediatR, ASP.NET
Hi, right now I’m creating same CRUD Commands/Queries(e.g. GetAll or DeleteById) for each new entity in my project. How to avoid that?
18 Replies
You probably could use a source generator.
You could also use generics:
That is, create a set of generic crud commands/queries and then use them with the entity type you're interested in at the time.
I’ve already tried that, but it didn’t work out because I had three generic parameters: class GetById<T, TDto, TId>; and I wasn’t able to register it in the DI.
What do you mean ?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
what type of project is this? because there's a good chance you shouldn't be creating all these models for all your entities
source: went through it
Admin panel for offline book shop
The thing is, I’m getting an error after trying to register service with 3 generic parameters
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
services.AddTransient(typeof(IRequestHandler<,>), typeof(GetAllEntites<,,,>);
This lines causes an error
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
Most likely, cause I got a bit lost with generics
That would be hard to do, cause I’ve deleted all generic queries/handlers
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
And which exact part of code do you need ?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
Let me ask different question, what are my options to avoid creating CRUD commands/queries each time?
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
I use EFCore + Automapper
So the best way is to create commands/handlers for each entity?
+ Generic repository
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
Fair enough, thanks!