使用通用存储库实现简单注入器 [英] Implement Simple Injector with generic repository
问题描述
我有一个接口 IRepository< T>其中T:class
和一个抽象类抽象类Repository< C,T> :IRepository< T>其中T:class用于实现接口的C:DbContext
。最后,我有我的实体存储库,它继承了抽象类。下面是一个混合的例子:
public interface IRepository< T>其中T:class
{
IQueryable< T>得到所有();
IQueryable< T> FindBy(Expression< Func< T,bool>>谓词);
void Add(T entity);
无效删除(T实体);
}
公共抽象类库< 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; }
}
公共虚拟IQueryable< T> GetAll()
{
IQueryable< T> query = _context.Set< T>();
返回查询;
}
...
}
公共类PortalRepository:存储库< SequoiaEntities,Portal>
{
}
在我的global.asax.cs文件中,Application_Start ()函数,我补充道:
Container container = new Container();
container.Register< IRepository< Portal>,Repository< SequoiaEntities,Portal>>();
container.Verify();
当我启动我的项目时,Simple Injector会尝试验证容器,并且出现错误: p>
其他信息:给定类型
存储库< SequoiaEntities,Portal>
不是具体类型。请使用其他重载注册此类型。
有没有一种方法可以用泛型类实现Simple Injector,或者我必须通过特定的类?
注册< TService,TImpementation>()
方法允许你指定一个具体类型( TImplementation
),当指定的服务( TService $ c $ )被请求。然而,指定的实现
Repository< SequoiaEntities,Portal>
被标记为 abstract 。这不允许Simple Injector创建它;抽象类无法创建。 CLR不允许这样做。
然而,您确实有一个具体的类型 PortalRepository
,我相信这是您的返回该类型的目标。因此,您的配置应如下所示:
container.Register< IRepository< Portal> ;, PortalRepository>();
或者,您可以使用Simple Injector的批量注册功能并将所有存储库注册到一个调用中:
Assembly [] assemblies = new [] {typeof(PortalRepository).Assembly};
container.Register(typeof(IRepository<>),assemblies);
I have some problem to implement SimpleInjector with my generic Repository.
I have an interface IRepository<T> where T : class
and an abstract class abstract class Repository<C, T> : IRepository<T> where T : class where C : DbContext
which implements the interface. Finally I have my entity repositories which inherit the abstract class. Here's a concret example:
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>
{
}
In my global.asax.cs file, under Application_Start() function, I added :
Container container = new Container();
container.Register<IRepository<Portal>, Repository<SequoiaEntities, Portal>>();
container.Verify();
When I launch my project, Simple Injector tries to verify the container and I get an error :
Additional information: The given type
Repository<SequoiaEntities, Portal>
is not a concrete type. Please use one of the other overloads to register this type.
Is there a way to implement Simple Injector with generic class or I have to pass by specific class?
The Register<TService, TImpementation>()
method allows you to specify a concrete type (TImplementation
) that will be created by Simple Injector when the specified service (TService
) is requested. The specified implementation Repository<SequoiaEntities, Portal>
however is marked as abstract
. This disallows Simple Injector from creating it; abstract classes can not be created. The CLR does not permit this.
You do have a concrete type PortalRepository
however, and I believe it is your goal to return that type. Your configuration should therefore look as follows:
container.Register<IRepository<Portal>, PortalRepository>();
Alternatively, you can make use of Simple Injector's batch-registration facilities and register all your repositories in one call:
Assembly[] assemblies = new[] { typeof(PortalRepository).Assembly };
container.Register(typeof(IRepository<>), assemblies);
这篇关于使用通用存储库实现简单注入器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!