Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting an IEnumerable into a constructor with a Ninject factory method

I'm trying to inject an IEnumerable into a constructor with Ninject.

My constructor looks like this:

public MatrixViewModel(IEnumerable<FooViewModel> fooViewModels)
{
    _fooViewModels = fooViewModels;
}

My Ninject module looks like this:

public class MainModule : NinjectModule
{
    public override void Load()
    {
        Bind<IEnumerable<FooViewModel>>()
            .ToMethod(context => GetFooViewModels())
            .InSingletonScope(); // this binding is not working
    }

    private IEnumerable<FooViewModel> GetFooViewModels()
    {
        // returns a bunch of foo view models
    }
}

This doesn't seem to be working. I don't get any error. Ninject just doesn't ever use the binding, and the value that is passed into the constructor is basically just an empty default value.

How do you inject an IEnumerable with Ninject?

Edit

More details on my factory method:

private IEnumerable<FooViewModel> GetFooViewModels()
{
    return new[]
    {
        new FooViewModel
        {
            Bar = new BarViewModel
            {
                X = 1,
                Y = 2
            },
            Misc = "Hello"
        },
        new FooViewModel
        {
            Bar = new BarViewModel
            {
                X = 3,
                Y = 4
            },
            Misc = "Goodbye"
        },
        // etc.....
    };
}

Edit 2

Based on Remo's answer, one possible solution is to use a foreach loop to bind the view models one at a time:

foreach (var fooViewModel in GetFooViewModels())
{
    Bind<FooViewModel>().ToConstant(fooViewModel);
}
like image 502
devuxer Avatar asked Oct 19 '11 05:10

devuxer


3 Answers

Enumerables are treated differently by Ninject. Just provide bindings for all the view models. For enumerables Ninject will create an instance of every applying binding and pass them as IEnumerable.

e.g.

Bind<FooViewModel>().To<FooViewModel1>();
Bind<FooViewModel>().To<FooViewModel2>();
like image 161
Remo Gloor Avatar answered Nov 11 '22 13:11

Remo Gloor


Based on Remo's answer, one possible solution is to use a foreach loop to bind the view models one at a time:

foreach (var fooViewModel in GetFooViewModels())
{
    Bind<FooViewModel>().ToConstant(fooViewModel);
}
like image 38
devuxer Avatar answered Nov 11 '22 14:11

devuxer


From your question:

Bind<IEnumerable<FooViewModel>>()
    .ToMethod(context => GetFooViewModels())

I am not sure collection support or ToMethod works that way.

This should would work though:

Bind<MatrixViewModel>()
    .ToMethod(context => new MatrixViewModel(GetFooViewModels()))

Of course, how useful this solution is depends on how you're building up your views.

like image 4
Merlyn Morgan-Graham Avatar answered Nov 11 '22 12:11

Merlyn Morgan-Graham