Question

Is there a simple way to use Ninject to bind all Factory interfaces to the ToFactory() extention method?

public class Foo
{
    readonly IBarFactory barFactory;

    public Foo(IBarFactory barFactory)
    {
        this.barFactory = barFactory;
    }

    public void Do()
    {
        var bar = this.barFactory.CreateBar();
        ...
    }
}

public interface IBarFactory
{
    Bar CreateBar();
}

For the code above I could use:

kernel.Bind<IBarFactory>().ToFactory();

What would I do though if I had 10 or 20 IFactory interfaces that needed binding?

Was it helpful?

Solution

Yes use conventions: https://github.com/ninject/ninject.extensions.conventions

this.Bind(x => x.From_UseSomeOverloadToSpecifyTheAssemblies()
                .SelectAllInterfaces().EndingWith("Factory")
                .BindToFactory();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top