Search code examples
singletoncastle-windsortyped-factory-facility

Windsor Typed Factory facility, passing arguments when using singleton lifestyle


I am using Castle Windsor.

I have two component types where the implementation can be selected at runtime on a GUI. To handle this, I am resolving them by name. To handle resolving them by name, I am using the Typed Factory Facility.

One of the component types depends on the other. To handle the dependency, I am passing the argument as a factory method/constructor parameter argument.

Here is a redacted and abridged version of this factory interface:

public interface IModelFactory
{
    IMyDomainCommandFactory GetFooCommandFactory();
    IMyDomainCommandFactory GetBarCommandFactory();
    IMyDomainStrategy GetCreateSpecificSizeStrategy(int size, IMyDomainCommandFactory commandFactory);
    IMyDomainStrategy GetCreateUntilFailureStrategy(IMyDomainCommandFactory commandFactory);
}

Note that I am using my own implementations for IMyDomainCommandFactory, rather than using the Typed Factory facility. Those factories have intentionally complex behavior, and the facility doesn't suit their needs.

The problem I am noticing is that if I register my strategy components with a singleton lifestyle, I always get back the same instance, even if I pass different arguments to the getter.

In my opinion, this goes against the Principal of Least Astonishment, but maybe other people have a different opinion :) Should this be considered a bug?

If not, is there a clean way to get the container or factory to create only one instance per argument combination?


Solution

  • Depending how you look at it, but certainly instance per combination of parameters can't be called a singleton so I say it would go against PoLA if Windsor did implement the behavior you'd expect.

    If you want it, you need a custom, non-singleton lifestyle.