Frameworks 使用实体框架和Castle Windsor容器解决通用存储库问题

Frameworks 使用实体框架和Castle Windsor容器解决通用存储库问题,frameworks,repository,entity,generics,inversion-of-control,Frameworks,Repository,Entity,Generics,Inversion Of Control,我正在使用v4的通用存储库实现中工作,存储库必须由 首先是界面, public interface IRepository<T> { void Add(T entity); void Delete(T entity); T Find(int key) } 现在的问题是,正如您所看到的,我正在使用一个实体框架接口,比如ioobjectset,来完成这项工作,但是这种类型需要对T泛型类型“where T:class”进行约束 当Windsor试图解析其具体类型时

我正在使用v4的通用存储库实现中工作,存储库必须由

首先是界面,

public interface IRepository<T>
{
    void Add(T entity);
    void Delete(T entity);
    T Find(int key)
}
现在的问题是,正如您所看到的,我正在使用一个实体框架接口,比如
ioobjectset
,来完成这项工作,但是这种类型需要对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>
如果我删除了具体类中的约束和对IObjectSet的依赖(如果我不这样做,我会得到一个编译错误),一切都会正常工作,所以我认为这不是容器问题,但是
IObjectSet
在实现中是必须的


如何解决此问题?

这是很久以前的事了,但解决方法之一是对界面施加相同的约束

public interface IRepository<T> where T : class
公共接口i假设,其中T:class
<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'.
public interface IRepository<T> where T : class