❔ ✅ Generic-ify adding scoped for entities
So I've created a repository pattern based logic of dealing with DB entities.
Basically I can do
IRepository<WhateverClassIWant>()
to connect the corresponding entity in the database, issue with this is that on startup, I need to add scopped to all my class:
All the above models inherit from a base class called BaseEntity
that has some core fields such as Id, CreatedOn, UpdatedOn, etc
I want to make this services.AddScoped
action generic so I don't need to define this every time I create a new database entity.
This is my current status which is not working:
Directly feeding BaseType is not working either, since IRepository requires a type of T : BaseEntity
, not a type of type type
7 Replies
Also have tried this:
You can't cast types like
(BaseEntity)BaseType
You'll need to invoke the AddScoped()
method with reflections as well
(or better yet, ditch the repository pattern)Yes, I have realized that now, but was worth a shot, as I've gotten so frustrated with this at this point 😆
I'm actually somewhat convinced this isn't a repository pattern anymore, just has the name stuck on it
It's very different from a traditional repository pattern
googling, will be back
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Alright, so the solution to this was much simpler than I thought @Angius
Basically, it's just
This is it
That works
Thanks for the help
I came across this on the NopCommerce repo
https://github.com/nopSolutions/nopCommerce/blob/72657ad4d09f2c1a413dac5c4e780473ac5990ca/src/Presentation/Nop.Web.Framework/Infrastructure/NopStartup.cs
Line 239
GitHub
nopCommerce/NopStartup.cs at 72657ad4d09f2c1a413dac5c4e780473ac5990...
ASP.NET Core eCommerce software. nopCommerce is a free and open-source shopping cart. - nopCommerce/NopStartup.cs at 72657ad4d09f2c1a413dac5c4e780473ac5990ca · nopSolutions/nopCommerce
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.