相关疑难解决方法(0)

如何在ASP.NET Core中注入泛型的依赖关系

我有以下存储库类:

public class TestRepository : Repository<Test>
{
    private TestContext _context;

    public TestRepository(TestContext context) : base(context)
    {
        _context = context;
    }
}

public abstract class Repository<T> : IRepository<T> where T : Entity
{
    private TestContext _context;

    public Repository(TestContext context)
    {
        _context = context;
    }
    ...
}

public interface IRepository<T>    
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

如何在我的ASP.NET Core中实现依赖注入Startup.cs

我这样实现了:

services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
Run Code Online (Sandbox Code Playgroud)

但后来我得到以下错误:

无法实例化实现类型'Test.Domain.Repository 1[T]' for service type 'Test.Domain.IRepository1 [T]'.

c# generics dependency-injection asp.net-core

8
推荐指数
2
解决办法
8137
查看次数

标签 统计

asp.net-core ×1

c# ×1

dependency-injection ×1

generics ×1