Search code examples
c#dependency-injectioninversion-of-controlcastle-windsorioc-container

DI - Assembly wide install based on generic interface does not register its implementations


I am trying to register Interfaces and their implementations using Castle Windsor.

I have a Class called ProductRiskStatusChecks which implements IChecks<IProductRisk> where IProductRisk implements IValidateDomainObject

I tried to install these as below -

public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Classes.FromAssemblyNamed("OpenBoxExtraBusinessLogic")
                    .BasedOn<IChecks<IValidateDomainObject>>()
                    .WithServiceAllInterfaces().LifestyleSingleton());
        }

but it doesn't install ProductRiskChecks against IChecks<IProductRisk> but if I do a explicit register as below it works fine

container.Register(Component.For<IChecks<IProductRisk>>().ImplementedBy<ProductRiskChecks>());

Can I here know why isn't first case registering my Checks class ?

Thanks


Solution

  • Your registration does not work because of type mismatch (exactly as Alexei mentioned).

    I'm not exactly sure what you want to achieve here but if you have multiple implementations of IChecks<> each with different generic parameter and you want to register them all you can go with registration using open generics:

    container.Register(Classes.FromAssemblyNamed("OpenBoxExtraBusinessLogic")
                          .BasedOn(typeof(ICheck<>))
                          .WithServiceAllInterfaces()
                          .LifestyleSingleton())