Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NInject, resolving IEnumerable<T> fails when injecting a a ctor arg, but not when doing Get<IEnumerable<T>>()

When using NInject if I set up a binding for IEnumerable, it will work if I directly request an IEnumerable, but not if another bound object requires an IEnumerable. Is this by design?

class Program {
    static void Main(string[] args){
        var k = new StandardKernel();
        k.Bind<IEnumerable<int>>().ToMethod(GetInts);
        k.Bind<IFoo>().To<Foo>(); //Has an IEnumberable<int> constructor arg
        var works = k.Get<IEnumerable<int>>(); //returns the array of ints
        var tst = k.Get<IFoo>(); //Empty integer array is passed in by ninject???
        tst.Get(); //returns an empty integer array????
        return;
    }

    public static int[] GetInts(IContext ctx){
        return new int[] {1,2,3,4,5};
    }
}


public interface IFoo{
    IEnumerable<int> Get();
}


public class Foo : IFoo{
    private int[] _vals;

    public Foo(IEnumerable<int> vals){
        _vals = vals.ToArray();
    }

    public IEnumerable<int> Get(){
        return _vals;
    }
}
like image 260
jowensboggs Avatar asked Oct 05 '22 07:10

jowensboggs


1 Answers

You're seeing Multi Injection happening.

You could override it by adding an explicit Get to your binding:

k.Bind<IFoo>().To<Foo>()
    .WithConstructorArgument( "vals", ctx=>ctx.Kernel.Get<IEnumerable<int>>());

(Though if you look at the source, I'm sure you'll find a way to inhibit Multi Injection.)

like image 92
Ruben Bartelink Avatar answered Oct 16 '22 20:10

Ruben Bartelink