4
votes

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?

1

1 Answers

6
votes

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

this.Bind(x => x.From_UseSomeOverloadToSpecifyTheAssemblies()
                .SelectAllInterfaces().EndingWith("Factory")
                .BindToFactory();