我的一般问题是使用SimpleInjector来实现我的通用资源库。使用通用资源库实现简单注入器
我有一个接口IRepository<T> where T : class
和一个实现接口的抽象类abstract class Repository<C, T> : IRepository<T> where T : class where 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>
{
}
在我的Global.asax.cs文件,下的Application_Start()函数,我说:
Container container = new Container();
container.Register<IRepository<Portal>, Repository<SequoiaEntities, Portal>>();
container.Verify();
当我启动我的项目,简单的注射器试图验证容器并且出现错误:
其他信息:给定类型
Repository<SequoiaEntities, Portal>
不是具体类型。请使用其他重载之一来注册此类型。
有没有一种方法来实现简单的喷油器与泛型类或我必须通过特定的类?
您应该注册不是抽象的类型,但最后一个:PortalRepository:* container.Register,PortalRepository>(); *抽象类的问题是它不能被实例化。 –
3615
@ 3615你应该把它作为一个答案 – Nkosi