07-20-2023, 05:44 AM
I'm working in a generic repository implementarion with [Entity Framework][1] v4, and the repository must be resolved by a [Windsor container][2].
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, but
`IObjectSet` is a MUST in the implementation.
How do I fix this problem?
[1]:
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, but
`IObjectSet` is a MUST in the implementation.
How do I fix this problem?
[1]:
[To see links please register here]
[2]:[To see links please register here]