C# 使用通用存储库实现简单注入器

C# 使用通用存储库实现简单注入器,c#,generics,dependency-injection,simple-injector,C#,Generics,Dependency Injection,Simple Injector,我在用我的通用存储库实现SimpleInjector时遇到了一些问题 我有一个接口IRepository,其中T:class和一个抽象类抽象类存储库:IRepository,其中T:class,其中C:DbContext,它实现了接口。最后,我有了继承抽象类的实体存储库。下面是一个具体的例子: public interface IRepository<T> where T : class { IQueryable<T> GetAll(); IQueryab

我在用我的通用存储库实现SimpleInjector时遇到了一些问题

我有一个接口
IRepository,其中T:class
和一个抽象类
抽象类存储库:IRepository,其中T:class,其中C:DbContext
,它实现了接口。最后,我有了继承抽象类的实体存储库。下面是一个具体的例子:

public interface IRepository<T> where T : class
{
    IQueryable<T> GetAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
    void Add(T entity);
    void Remove(T entity);
}


public abstract class Repository<C, T> : IRepository<T> 
    where T : class where C : DbContext, new()
{
    private C _context = new C();
    public C Context
    {
        get { return _context; }
        set { _context = value; }
    }
    public virtual IQueryable<T> GetAll()
    {
        IQueryable<T> query = _context.Set<T>();
        return query;
    }
    ...
}

public class PortalRepository : Repository<SequoiaEntities, Portal>
{
}
公共接口i假设,其中T:class
{
IQueryable GetAll();
IQueryable FindBy(表达式谓词);
无效添加(T实体);
无效删除(T实体);
}
公共抽象类存储库:IRepository
其中T:class,其中C:DbContext,new()
{
私有C_上下文=新C();
公共C上下文
{
获取{return\u context;}
设置{u context=value;}
}
公共虚拟IQueryable GetAll()
{
IQueryable查询=_context.Set();
返回查询;
}
...
}
公共类PortalRepository:存储库
{
}
在我的global.asax.cs文件的Application_Start()函数下,我添加了:

Container container = new Container();
container.Register<IRepository<Portal>, Repository<SequoiaEntities, Portal>>();
container.Verify();
Container=newcontainer();
container.Register();
container.Verify();
当我启动我的项目时,Simple Injector尝试验证容器,我得到一个错误:

附加信息:给定类型
存储库
不是具体类型。请使用其他重载之一注册此类型

有没有一种方法可以用泛型类实现Simple Injector,或者我必须通过特定类来实现?

当请求指定的服务(
TService
)时,
Register()
方法允许您指定Simple Injector将创建的具体类型(
TImplementation
)。但是,指定的实现
存储库
标记为
抽象
。这不允许简单的注入器创建它;无法创建抽象类。CLR不允许这样做

但是,您确实有一个具体的类型
PortalRepository
,我相信返回该类型是您的目标。因此,您的配置应如下所示:

container.Register<IRepository<Portal>, PortalRepository>();
container.Register();
或者,您可以使用Simple Injector的批注册功能,在一次调用中注册所有存储库:

Assembly[] assemblies = new[] { typeof(PortalRepository).Assembly };

container.Register(typeof(IRepository<>), assemblies);
Assembly[]assemblies=new[]{typeof(PortalRepository).Assembly};
容器。寄存器(类型(IRepository),组件);
当请求指定的服务(
t服务
)时,
Register()
方法允许您指定一个具体类型(
t实现
),该类型将由Simple Injector创建。但是,指定的实现
存储库
标记为
抽象
。这不允许简单的注入器创建它;无法创建抽象类。CLR不允许这样做

但是,您确实有一个具体的类型
PortalRepository
,我相信返回该类型是您的目标。因此,您的配置应如下所示:

container.Register<IRepository<Portal>, PortalRepository>();
container.Register();
或者,您可以使用Simple Injector的批注册功能,在一次调用中注册所有存储库:

Assembly[] assemblies = new[] { typeof(PortalRepository).Assembly };

container.Register(typeof(IRepository<>), assemblies);
Assembly[]assemblies=new[]{typeof(PortalRepository).Assembly};
容器。寄存器(类型(IRepository),组件);

您不应该注册抽象类型,而应该注册最终类型:PortalRepository:container.register();抽象类的问题是它不能被实例化。@3615您应该把它作为一个答案,您不应该注册抽象类型,而应该注册最终类型:PortalRepository:container.register();抽象类的问题是它不能被实例化。@3615在第二种情况下,`typeof(PortalRepository)`,它仅适用于PortalRepository类,但您谈论的是注册所有存储库?@DervisFindik该示例显示了在同一程序集中注册所有存储库,与
PortalRepository
所在的位置相同。在第二种情况下,`typeof(PortalRepository),它仅适用于PortalRepository类,但您谈论的是注册所有存储库?@DervisFindik该示例显示了在同一程序集中注册所有存储库,与
PortalRepository
所在的位置相同。