如何添加通用依赖注入 [英] How to add a generic dependency injection
问题描述
存储库接口: $ b $
使用只读api服务并使用泛型将操作打包为基于约定的过程。 b public interface IRepository< TIdType,TEntityType>其中TEntityType:class {
Task< EntityMetadata< TIdType>> GetMetaAsync();
$ / code>
存储库实现:
public class Repository< TIdType,TEntityType> :IRepository< TIdType,TEntityType>其中TEntityType:class {
public Repository(string connectionString){// initialization}
public async Tas< EntityMetadata< TIdType>> GetMetaAsync(){//实现}
}
在启动.cs - > ConfigureServices
:
services.AddSingleton< IRepository< int,Employee>> (p => new Repository< int,Employee>(connectionString));
services.AddSingleton< IRepository< int,Department>> (p => new Repository< int,Department>(connectionString));
//等等
控制器:
public class EmployeeController:Controller {
public EmployeeController(IRepository< int,Employee> repo){// stuff}
}
我正在为 ConfigureServices $ c中的所有类型的实体类型重复存储库implmentation $ C>。
services.AddSingleton< IRepository< TIdType,TEntityType>> (p => new Repository< TIdType,TEntityType>(connectionString));
那么在控制器的构造函数调用中可以自动获取相关的存储库?
更新1 :不是重复:
- 存储库实现没有默认构造函数
- 因为它没有默认构造函数,所以我不能提供在
- 在尝试
services.AddScoped(typeof(IRepository<>),...)
时出现错误使用泛型类型'IRepostiory< TIdType,TEntityType>'需要2个类型参数
由于此问题仍未正确标记为 <$ c $注意一个Generic类的方法:
现在您可以通过以下方式解决它:
serviceProvider.GetService(typeof(IRepository< A,B>));
//或:with extensionmethod
serviceProvider.GetService< IRepository< A,B>>();
Working on a read-only api service and making use of generics to package the operation into convention based process.
Repository interface:
public interface IRepository<TIdType,TEntityType> where TEntityType:class {
Task<EntityMetadata<TIdType>> GetMetaAsync();
}
Repository implementation:
public class Repository<TIdType,TEntityType> : IRepository<TIdType,TEntityType> where TEntityType:class {
public Repository(string connectionString) { // initialization }
public async Tas<EntityMetadata<TIdType>> GetMetaAsync() { // implementation }
}
In Startup.cs -> ConfigureServices
:
services.AddSingleton<IRepository<int, Employee>> ( p=> new Repository<int, Employee>(connectionString));
services.AddSingleton<IRepository<int, Department>> ( p=> new Repository<int, Department>(connectionString));
// and so on
Controller:
public class EmployeeController : Controller {
public EmployeeController(IRepository<int,Employee> repo) {//stuff}
}
I am currently repeating the repository implmentation for all types of entity types in the ConfigureServices
. Is there a way to make this generic too?
services.AddSingleton<IRepository<TIdType, TEntityType>> ( p=> new Repository<TIdType, TEntityType>(connectionString));
so in the controller constructor call can automatically get the relevant repository?
Update 1: Not a duplicate:
- The repository implementation does not have default constructor
- Because it does not have default constructor, I cannot provide the solution given in the linked question.
- When trying
services.AddScoped(typeof(IRepository<>), ...)
I am getting errorUsing the generic type 'IRepostiory<TIdType,TEntityType>' requires 2 type arguments
Since this question is still not properly marked as duplicate: The way to register a Generic class:
services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
now you can resolve it in the following way:
serviceProvider.GetService(typeof(IRepository<A,B>));
// or: with extensionmethod
serviceProvider.GetService<IRepository<A,B>>();
这篇关于如何添加通用依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!