Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 426 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem resolving a generic repository with Entity Framework and a Castle Windsor container

#1
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]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through