Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 泛型类的寄存器依赖注入_C#_Asp.net Core - Fatal编程技术网

C# 泛型类的寄存器依赖注入

C# 泛型类的寄存器依赖注入,c#,asp.net-core,C#,Asp.net Core,下面是我的应用程序结构: 应用 寄宿者 网 服务 我有这样的存储库代码 public interface IRepository<T> { } public class Repository <T> { } public interface ITestRepository : IRepository<Model> { } public class TestRepository : Repository<Model>, ITestR

下面是我的应用程序结构:

  • 应用
    • 寄宿者
    • 服务
我有这样的存储库代码

public interface IRepository<T> { }

public class Repository <T> { }

public interface ITestRepository : IRepository<Model> { }

public class TestRepository : Repository<Model>, ITestRepository { }

public interface ITest2Repository : IRepository<Model2> { }

public class Test2Repository : Repository<Model2>, ITest2Repository { }
现在,我正在
Startup.cs
中注册存储库,如下所示

services.AddScoped(typeof(IRepository<Model>), typeof(TestRepository));
services.AddScoped(typeof(IRepository<Model2>), typeof(Test2Repository));
services.AddScoped<ITestRepository, TestRepository>();
services.AddScoped<ITest2Repository, Test2Repository>();

TL;DR,有没有简化该存储库的注册的解决方案?

您可以添加自己的扩展方法来简化注册:

static class ServiceCollectionExtensions
{
    public static void AddRepository<TInterface, TRepository, TModel>(this IServiceCollection serviceCollection) 
        where TInterface : IRepository<TModel> 
        where TRepository : TInterface
    {
        services.AddScoped<TInterface, TRepository>();
        services.AddScoped<IRepository<TModel>, TRepository>();
    }
}

// usage:
services.AddRepository<ITestRepository, TestRepository, Model>();
静态类ServiceCollectionExtensions
{
公共静态void AddRepository(此IServiceCollection服务集合)
TInterface的位置:IRepository
地点:TInterface
{
services.addScope();
services.addScope();
}
}
//用法:
services.AddRepository();
关于您提到的错误,请参见Kirk的评论:


看起来
Repository
实际上并没有实现
IRepository
——我可以理解为什么DI系统对此不满意


TestRepository
的构造函数是什么样子的?看起来
Repository
实际上没有实现
IRepository
-我可以理解DI系统为什么对此不满意。@haim770如下:
publictestrepository(ApplicationContext上下文):base(context){}
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<ITestRepository, TestRepository>();
services.AddScoped<ITest2Repository, Test2Repository>();
static class ServiceCollectionExtensions
{
    public static void AddRepository<TInterface, TRepository, TModel>(this IServiceCollection serviceCollection) 
        where TInterface : IRepository<TModel> 
        where TRepository : TInterface
    {
        services.AddScoped<TInterface, TRepository>();
        services.AddScoped<IRepository<TModel>, TRepository>();
    }
}

// usage:
services.AddRepository<ITestRepository, TestRepository, Model>();