Suppose I have an interface:
public interface IService
{
void DoThing();
}
Now let's say I want to have a few implementations of this, so I can swap out which implmentation I want to use at runtime:
container.Register(Component.For<IService>().ImplementedBy<IServiceImplementationOne>().Named("First"));
container.Register(Component.For<IService>().ImplementedBy<IServiceImplementationTwo>().Named("Second"));
container.Register(Component.For<IService>().ImplementedBy<IServiceImplementationThree>().Named("Third"));
So I would be able to pick one by name:
container.Resolve<IService>("First"); //returns IServiceImplementationOne
container.Resolve<IService>("Second"); //returns IServiceImplementationTwo
container.Resolve<IService>("Third"); //returns IServiceImplementationThree
Now I'd like to be able to use the built in type facility to pick which one I want at runtime and not have more code sitting in my codebase if Windsor can do this automagically.
So let's say I have a factory service now:
public interface IServiceFactory
{
IService Get(string name);
}
If I register that like so:
container.Register(Component.For<IServiceFactory>().AsFactory());
If I try calling Get("First")
I get an exception saying it can't find a registration named ''
.
According to the docs, anything after Get
in your method name will be used as the name to resolve, so you can use GetXXX()
in your factory interface to boil it down to container.Resolve<IService>("XXX")
, and I was hoping that just using Get
as the method name to let me specify that at runtime, but apparently that is not the case.
The docs also say you can use the word Create
in your method names to be able to forward constructor arguments to the component created by the factory, but that's not what I want either.
Does the built in typed factory allow for this behaviour? I realise I can implement a custom ITypedFactoryComponentSelector
but if I'm going that far then I may as well just implement my own concrete factory class anyway.