Problem resolving a generic Repository with Entity Framework and Castle Windsor Container
- by user368776
Hi, im working in a generic repository implementarion with EF v4, the repository must be resolved by 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 im using a EF interface like IObjectSet to do the work, but this type requires a constraint for the T generic type "where T: class".
That constrait is causing an exception when Windsor tries to resolve its concrete type. 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>
The container resolve code
IRepository<Product> productsRep =_container.Resolve<IRepository<Product>>();
Now the exception im gettin
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 depedency on IObjectSet (if i dont do it get a compile error) everything works FINE, so i dont think is a container issue, but
IObjectSet is a MUST in the implementation.
Some help with this, please.