简体   繁体   中英

Dependency Injection for Generic Repository

what is the way to register a Generic class with 3 arguments

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. 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. You are registering interface as interface , so it cannot be instantiated .

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

Should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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