views:

47

answers:

1

Considering this code :

interface IRepository<T>
{
    void Save();
}

class Repository<T>
{
    public virtual void Save() // something 
    { }
}

interface IOtherRepository : IRepository<OtherClass>
{
    void Other();
}

class OtherRepository : Repository<OtherClass>, IOtherRepository
{
    public override void Save() // something different
    { }

    public override void Other(){ }
}

How is it possible to configure Castle Windsor to give me an instance of OtherRepository when I call container.Resolve<IRepository<OtherClass>> ?

If Castle Windsor can't do this, which ioc containers can ?

+1  A: 
var container = new WindsorContainer();
container.Register(Component.For(typeof(IRepository<>))
                            .ImplementedBy(typeof(Repository<>));
container.Register(Component.For<IRepository<OtherClass>, IOtherRepository>()
                            .ImplementedBy<OtherRepository>());
var repo = container.Resolve<IRepository<Something>>();
Assert.IsInstanceOfType(typeof(Repository<Something>), repo);
var specificRepo = container.Resolve<IRepository<OtherClass>>();
Assert.IsInstanceOfType(typeof(OtherRepository), specificRepo);
var otherRepo = container.Resolve<IOtherRepository>();
Assert.AreSame(otherRepo, specificRepo);
Mauricio Scheffer
I edited my question to add the IOtherRepository I forgot. Works well too. I should have tested this before asking... Thanks
mathieu

related questions