1
votes

I want to select a particular binding based off a configuration value, for arguments sake, let's say I have two concrete classes called Action1 and Action2. Currently I am passing in the Ninject kernel into a factory and returning the bound type based off that.

 public class ActionFactory: IActionFactory
    {
        public IAction GetAction(IKernel kernel, string actionName)
        {
            return kernel.Get<IAction>(actionName);
        }
    }

I am binding the actions in a Ninject Module like so:

Bind<IAction>().To<Action1>().Named("Action1");
Bind<IAction>().To<Action2>().Named("Action2");

Would it be possible for me to bind this without having to pass the kernel to a factory?

I do have the ability to refactor the workflow too, if anyone has any suggestions.

1

1 Answers

1
votes

Directly inject the configured action and configure them conditional:

Bind<IAction>().To<Action1>().When(ctx => IsAction1Configured());
Bind<IAction>().To<Action2>().When(ctx => IsAction2Configured());