Christian Junk
Christian Junk

Reputation: 1010

Resolving Generics with Castle Windsor

I'm having problems resolving generic interfaces/classes using Windsor Castle 2.5. I have the following classes and interfaces:

public interface IGenericDao<T> : IDao
public abstract class GenericDao<T> : IGenericDao<T> where T:class, new()
public class LocationDao : GenericDao<Location>, ILocationDao

For the registration I am using the following lines of code

c.Register(
           Component.For(typeof(IGenericDao<>))
               .ImplementedBy(typeof(GenericDao<>)));

c.Register(Component.For<ILocationDao>().ImplementedBy<LocationDao>());

Now, when I'm trying to resolve ILocationDao

var i = c.Resolve<ILocationDao>();

the return value is a Castle.Proxies.ILocationDaoProxy and I'm not able to cast it to a LocationDao.

When I remove GenericDao<Location> from class `LocationDao'

public class LocationDao : ILocationDao

everything works fine. So I guess that I'm doing something wrong with the registration.

Could you please help me?

Upvotes: 1

Views: 331

Answers (1)

svick
svick

Reputation: 244837

If you're telling the container that you want ILocationDao, you shouldn't cast the result to LocationDao, just use it as ILocationDao.

If you are sure using the interface is not enough for you, then register LocationDao as LocationDao and resolve that.

Upvotes: 2

Related Questions