通用存储库的依赖注入

JRA*_*JRA 1 c# asp.net-mvc unity-container asp.net-core

用3个参数注册通用类的方法是什么

public interface ITest<T,V,VE>
{

}

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

}
Run Code Online (Sandbox Code Playgroud)

我是这样注册的

services.AddScoped(typeof(ITest<,,>), typeof(ITest<,,>));
Run Code Online (Sandbox Code Playgroud)

但无法进入构造函数以及

service.GetService(typeof(ITest<TestClass, vTestClass, VETestClass>)) as ITest<TestClass, vTestClass, VETestClass>;
Run Code Online (Sandbox Code Playgroud)

Cod*_*ler 5

问题在于AddScoped()方法的调用。您应该在第二个参数中传递实现类型,而不是接口本身的类型:

services.AddScoped(typeof(ITest<,,>), typeof(TestRespository<,,>));
Run Code Online (Sandbox Code Playgroud)