C
C#2y ago
RDasher

❔ ✅ 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:
services.AddScoped<IRepository<Product>, Repository<Product>>();
services.AddScoped<IRepository<User>, Repository<User>>();
services.AddScoped<IRepository<Order>, Repository<Order>>();
services.AddScoped<IRepository<Product>, Repository<Product>>();
services.AddScoped<IRepository<User>, Repository<User>>();
services.AddScoped<IRepository<Order>, Repository<Order>>();
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:
foreach (BaseEntity BaseType in Assembly.GetAssembly(typeof(BaseEntity)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(BaseEntity))))
{
services.AddScoped <IRepository<(BaseEntity)BaseType>, Repository <(BaseEntity)BaseType>> ();
}
foreach (BaseEntity BaseType in Assembly.GetAssembly(typeof(BaseEntity)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(BaseEntity))))
{
services.AddScoped <IRepository<(BaseEntity)BaseType>, Repository <(BaseEntity)BaseType>> ();
}
Directly feeding BaseType is not working either, since IRepository requires a type of T : BaseEntity, not a type of type type
7 Replies
RDasher
RDasherOP2y ago
Also have tried this:
foreach (BaseEntity BaseType in Assembly.GetAssembly(typeof(BaseEntity)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(BaseEntity))))
{
services.AddScoped < IRepository < (BaseEntity)BaseType >, Repository < (BaseEntity)BaseType >> ();
}
foreach (BaseEntity BaseType in Assembly.GetAssembly(typeof(BaseEntity)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(BaseEntity))))
{
services.AddScoped < IRepository < (BaseEntity)BaseType >, Repository < (BaseEntity)BaseType >> ();
}
Angius
Angius2y ago
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)
RDasher
RDasherOP2y ago
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
Accord
Accord2y ago
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.
RDasher
RDasherOP2y ago
Alright, so the solution to this was much simpler than I thought @Angius Basically, it's just
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
This is it That works Thanks for the help
RDasher
RDasherOP2y ago
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
Accord
Accord2y ago
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.

Did you find this page helpful?