簡體   English   中英

通用存儲庫的依賴注入

[英]Dependency Injection for Generic Repository

用3個參數注冊泛型類的方法是什么

public interface ITest<T,V,VE>
{

}

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

}

我已經這樣注冊

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

但是無法進入構造器以及

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

問題在於調用AddScoped()方法。 您應該在第二個參數中傳遞實現的類型,而不是接口本身的類型:

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

您需要implementationinterface不兩次interface 您正在將interface注冊為interface ,因此無法instantiated

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

應該做到的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM