简体   繁体   English

通用存储库的依赖注入

[英]Dependency Injection for Generic Repository

what is the way to register a Generic class with 3 arguments 用3个参数注册泛型类的方法是什么

public interface ITest<T,V,VE>
{

}

public class TestRespository<T,V,VE>:ITest<T,V,VE>
{

}

i had registered like this 我已经这样注册

services.AddScoped(typeof(ITest<,,>), typeof(ITest<,,>));

but unable to get in Constructor as well as 但是无法进入构造器以及

service.GetService(typeof(ITest<TestClass, vTestClass, VETestClass>)) as ITest<TestClass, vTestClass, VETestClass>;

The problem is with call of AddScoped() method. 问题在于调用AddScoped()方法。 You should pass type of implementation in second argument, not the type of interface itself: 您应该在第二个参数中传递实现的类型,而不是接口本身的类型:

services.AddScoped(typeof(ITest<,,>), typeof(TestRespository<,,>));
services.AddScoped(typeof(ITest<,,>), typeof(ITest<,,>));

You need to have implementation and interface not interface twice. 您需要implementationinterface不两次interface You are registering interface as interface , so it cannot be instantiated . 您正在将interface注册为interface ,因此无法instantiated

services.AddScoped(typeof(ITest<,,>), typeof(TestRepository<,,>));

Should do the trick. 应该做到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM