Here is the example:
interface IComponentA {};
class ComponentA : IComponentA { };
interface IComponentB { };
class ComponentB : IComponentB { };
interface IComponentC { };
class ComponentC : IComponentC
{
public ComponentC(IComponentA a)
{
Console.WriteLine("Constructor A");
}
public ComponentC(IComponentB b)
{
Console.WriteLine("Constructor B");
}
};
All these components are registered in Castle Windsor container.
But class ComponentC
has 2 overloaded constructors. Any of them can be used when ComponentC
is being activated.
I need ComponentC(IComponentB b)
constructor to be used.
For a moment I'm using UsingFactoryMethod() method to resolve that:
container
.Register(Component
.For<IComponentA>()
.ImplementedBy<ComponentA>())
.Register(Component
.For<IComponentB>()
.ImplementedBy<ComponentB>())
.Register(Component
.For<IComponentC>()
.UsingFactoryMethod(() => new ComponentC(
container.Resolve<IComponentB>())));
It works, but probably Castle Windsor provides some better way to do that?
Any help is really appreciated.
Thanks.
ComponentB
doesn't have dependencies, consider skipping the registration ofIComponentB
/IComponentA
entirely. – DodderingISubDependencyResolver
) might also be useful. – DodderingComponentC(IComponentA)
createsIComponentB
by itself and don't useIComponentB
from container.ComponentC(IComponentB)
createsIComponentA
by itself and don't useIComponentA
from container. As a result the behavior ofComponentC
is different in these cases. – McandrewComponentC
should be registered in container twice (with different IDs): 1) using ctorComponentC(IComponentA)
2) using ctorComponentC(IComponentB)
– McandrewComponentC
then this component will use required ctor forComponentC
. But I'd like to have syntax similar toRegister(Component.For<IComponentC>().ImplementedBy<ComponentB>() .UsingConstructor<IComponentB>())
:) . Is there something similar in the Castle Windsor? – Mcandrew