使用实体框架和 Castle Windsor 容器解决通用存储库的问题
我正在使用 Entity Framework v4 进行通用存储库实现,并且存储库必须由 Windsor 容器 解决。
首先是接口,
public interface IRepository<T>
{
void Add(T entity);
void Delete(T entity);
T Find(int key)
}
然后是一个具体的类实现该接口
public class Repository<T> : IRepository<T> where T: class
{
private IObjectSet<T> _objectSet;
}
所以我需要 _objectSet
在上一个类中执行类似的操作,
public void Add(T entity)
{
_objectSet.AddObject(entity);
}
现在的问题是,如您所见,我正在使用实体框架接口类似于 IObjectSet 来完成这项工作,但此类型需要 T 泛型类型“where T: class”的约束。
当 Windsor 尝试解析其具体类型时,该约束会导致异常。温莎的配置如下所示。
<castle>
<components>
<component id="LVRepository"
service="Repository.Infraestructure.IRepository`1, Repository"
type="Repository.Infraestructure.Repository`1, Repository"
lifestyle="transient">
</component>
</components>
</castle>
容器解析代码:
IRepository<Product> productsRep =_container.Resolve<IRepository<Product>>();
现在我得到的异常:
System.ArgumentException: GenericArguments[0], 'T', on 'Repository.Infraestructure.Repository`1[T]' violates the constraint of type 'T'. ---> System.TypeLoadException: GenericArguments[0], 'T', on 'Repository.Infraestructure.Repository`1[T]' violates the constraint of type parameter 'T'.
如果我删除具体类中的约束和对 IObjectSet 的依赖(如果我不这样做,我会收到编译错误)一切都会正常工作,所以我不'不认为这是一个容器问题,但是 IObjectSet
是实现中必须的。
我该如何解决这个问题?
I'm working in a generic repository implementarion with Entity Framework v4, and the repository must be resolved by a Windsor container.
First the interface,
public interface IRepository<T>
{
void Add(T entity);
void Delete(T entity);
T Find(int key)
}
Then a concrete class implements the interface
public class Repository<T> : IRepository<T> where T: class
{
private IObjectSet<T> _objectSet;
}
So I need _objectSet
to do stuff like this in the previous class,
public void Add(T entity)
{
_objectSet.AddObject(entity);
}
And now the problem, as you can see, I'm using a Entity Framework interface like IObjectSet
to do the work, but this type requires a constraint for the T generic type "where T: class".
That constraint is causing an exception when Windsor tries to resolve its concrete type. The Windsor configuration look like this.
<castle>
<components>
<component id="LVRepository"
service="Repository.Infraestructure.IRepository`1, Repository"
type="Repository.Infraestructure.Repository`1, Repository"
lifestyle="transient">
</component>
</components>
</castle>
The container resolves code:
IRepository<Product> productsRep =_container.Resolve<IRepository<Product>>();
Now the exception I'm getting:
System.ArgumentException: GenericArguments[0], 'T', on 'Repository.Infraestructure.Repository`1[T]' violates the constraint of type 'T'. ---> System.TypeLoadException: GenericArguments[0], 'T', on 'Repository.Infraestructure.Repository`1[T]' violates the constraint of type parameter 'T'.
If I remove the constraint in the concrete class and the dependency on IObjectSet (if I don't do it, I get a compile error) everything works FINE, so I don't think it is a container issue, butIObjectSet
is a MUST in the implementation.
How do I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是很久以前的事了,但一种解决方法是对界面施加相同的约束。
It was a long time ago, but one work around is to put the same constraint on your interface.