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.