我有一个泛型类和通用接口是这样的:依赖注入的泛型类
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;
}
在我StartUp.cs我挂钩的类和接口
现在
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient(typeof(IDataService<>), typeof(DataService<>));
...
}
当我尝试在我的例子中使用它Repository.cs始终为空。
public class Repository<T> : IRepository<T> where T : class
{
private readonly IDataService<T> _dataService;
public Repository(IDataService<T> dataService)
{
_dataService = dataService;
...
}
...
}
编辑 这里是要求仓库接口和类
public interface IRepository<T> where T : class
{
double GetCallPrice(T callEntity, Enum billingType);
double GetCallPriceFromIdAndBillingType(int id, Enum billingType);
}
而且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();
...
}
...
}
你正在做的事情错了,你是不是在你的问题显示。此外,据我所知,内建的ASP.NET Core DI容器不允许向构造函数中注入“null”值。请说明你如何注册和解决'Repository',或者更确切地说:请创建一个[最小,完整和可验证的示例](https://stackoverflow.com/help/mcve)。 –
Steven