泛型类的依赖注入

Ovi*_*vis 7 c# asp.net dependency-injection asp.net-core

我有一个泛型类和这样的通用接口:

public interface IDataService<T> where T: class
{
    IEnumerable<T> GetAll();
}

public class DataService<T> : IDataService<T> where T : class
{
    public IEnumerable<T> GetAll()
    {
        return Seed<T>.Initialize();
    }
}

public static IEnumerable<T> Initialize()
{
    List<T> allCalls = new List<T>();
    ....
    return allCalls;
}
Run Code Online (Sandbox Code Playgroud)

现在在我的StartUp.cs中,我正在连接类和接口

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient(typeof(IDataService<>), typeof(DataService<>));
    ...
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的例如Repository.cs中使用它时,它总是为空.

public class Repository<T> : IRepository<T> where T : class
{
    private readonly IDataService<T> _dataService;

    public Repository(IDataService<T> dataService)
    {
        _dataService = dataService;
    ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

编辑这是请求的存储库接口和类

public interface IRepository<T> where T : class
{
    double GetCallPrice(T callEntity, Enum billingType);
    double GetCallPriceFromIdAndBillingType(int id, Enum billingType);
}
Run Code Online (Sandbox Code Playgroud)

和Repository.cs类

public class Repository<T> : IRepository<T> where T : class
{
    private readonly IDataService<T> _dataService;
    private IEnumerable<T> _allCalls;

    public Repository(IDataService<T> dataService)
    {
        _dataService = dataService;
    }

    public double GetCallPrice(int id)
    {
        _allCalls = _dataService.GetAllCalls();
        ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

J S*_*ott 12

Microsoft 的日志扩展在其服务集合配置中使用开放泛型

 services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));
Run Code Online (Sandbox Code Playgroud)

该行允许将记录器类的封闭版本注入为

public SomeService(ILogger<SomeService> logger)
{
    // ...
Run Code Online (Sandbox Code Playgroud)

测试表明,对于共享服务范围,每种不同类型T将映射到服务的不同实例,并且T可以请求的任何有效类型

如果您想尝试一下,请使用此测试台:https://dotnetfiddle.net/dVDqnj

此方法假设行为不是为接口的特定实现定义的,而是提供更多信息。如果您需要IGeneric<Foo>和的不同实现IGeneric<Bar>,则需要单独定义每个实现服务。


Aka*_*ava 9

services.AddTransient(typeof(IDataService<>), typeof(DataService<>));
Run Code Online (Sandbox Code Playgroud)

理想情况下,不应该允许这样做,但是当方法接受type作为参数时,它会在不执行任何验证的情况下接受它.没有人希望有人会尝试使用它.

它是null的原因,因为 typeof(IDataService<>) !== typeof(IDataService<SomeClass>)

您可以访问https://dotnetfiddle.net/8g9Bx7查看示例

这就是原因,DI解析器永远不会知道如何解决.大多数DI容器仅在类型实现请求的接口或具有基类作为请求的类时解析类型.

只有当A继承B或A实现B时,任何DI容器都将解析类型B的类型A.

在你的情况下,DataService<>实现IDataService<>,但DataService<T>没有实现IDataService<>

只有你可以使它工作的方法是为每种数据类型调用相同的方法

services.AddTransient(typeof(IDataService<Customer>), typeof(DataService<Customer>));

services.AddTransient(typeof(IDataService<Order>), typeof(DataService<Order>));

services.AddTransient(typeof(IDataService<Message>), typeof(DataService<Message>));
Run Code Online (Sandbox Code Playgroud)

要么

你可以创建一个ServiceFactory ......

interface IDataServiceFactory{
     DataService<T> Get<T>();
}

class DataServiceFactory : IDataServiceFactory{
     public DataService<T> Get<T>(){
          //.. your own logic of creating DataService

          return new DataService<T>();
     }
}
Run Code Online (Sandbox Code Playgroud)

并注册

services.AddTransient(typeof(IDataServiceFactory), typeof(DataServiceFactory));
Run Code Online (Sandbox Code Playgroud)