[英]Implement Simple Injector with generic repository
I have some problem to implement SimpleInjector with my generic Repository. 我在用通用存储库实现SimpleInjector时遇到一些问题。
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. 我有一个
IRepository<T> where T : class
接口IRepository<T> where T : class
和一个抽象类abstract class Repository<C, T> : IRepository<T> where T : class where C : DbContext
实现了该接口。 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 : 在我的global.asax.cs文件中的Application_Start()函数下,添加了:
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 : 当我启动项目时,Simple Injector尝试验证容器,但出现错误:
Additional information: The given type
Repository<SequoiaEntities, Portal>
is not a concrete type.附加信息:给定的
Repository<SequoiaEntities, Portal>
类型不是具体类型。 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? 有没有一种方法可以用泛型类实现Simple Injector或我必须通过特定的类传递?
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. Register<TService, TImpementation>()
方法允许您指定一个具体类型( TImplementation
),该类型将在请求指定服务( TService
)时由Simple Injector创建。 The specified implementation Repository<SequoiaEntities, Portal>
however is marked as abstract
. 但是,指定的实现
Repository<SequoiaEntities, Portal>
被标记为abstract
。 This disallows Simple Injector from creating it; 这不允许Simple Injector创建它; abstract classes can not be created.
无法创建抽象类。 The CLR does not permit this.
CLR不允许这样做。
You do have a concrete type PortalRepository
however, and I believe it is your goal to return that type. 但是,您确实有一个具体的类型
PortalRepository
,并且我相信返回该类型是您的目标。 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: 另外,您可以使用Simple Injector的批量注册功能,并在一个调用中注册所有存储库:
Assembly[] assemblies = new[] { typeof(PortalRepository).Assembly };
container.Register(typeof(IRepository<>), assemblies);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.