Search code examples
castle-windsor-3

How do I register default interfaces with Castle Windsor given an interface ancestor?


I have the following:

interface IAncestor { }

interface IDescendant1 : IAncestor { }

interface IDescendant2 : IAncestor { } 

class Descendant1 : IDescendant1 { }

class Descendant2 : IDescendant2 { }

What I would like to be able to do is automatically have Castle Windsor find all IDescendantX-DescendantX pairs without me having to specify them manually. Is this possible?

I've tried:

        container.Register(
            Classes.FromThisAssembly()
            .BasedOn<IAncestor>()
            .WithService.DefaultInterfaces()
            .LifestyleTransient()
        );

but this does not find the default interfaces. (I'm having trouble phrasing my question with the right terminology, so could not find a topic on SO that already answers this, sorry if it's a duplicate...)


Solution

  • Think the problem here is lack of access modifiers. If you add IncludeNonPublicTypes() the following test passes:

    [Test]
    public void Test() 
    {        
        //Arrange
        WindsorContainer sut = new WindsorContainer();
    
        //Act
        sut.Register(
                Classes.FromThisAssembly()
                .IncludeNonPublicTypes()
                .BasedOn<IAncestor>()
                .WithService.DefaultInterfaces()
                .LifestyleTransient());
    
        //Assert
        Assert.That(sut.Resolve<IDescendant1>(), Is.InstanceOf<Descendant1>());
        Assert.That(sut.Resolve<IDescendant2>(), Is.InstanceOf<Descendant2>());
    }