Search code examples
c#dependency-injectionninjectinversion-of-control

Configuring Ninject to return a list of results


I'm using Ninject to resolve dependencies and it's working swimmingly until now. In this example, I actually need a list of objects initialized based on data stored in my App.config.

However, Ninject keeps returning an empty list. The snippet below is an example of what I tried. I've included the constructors for the class hierarchy for some context.

public ServiceSchedulerBuilder(IList<ITextExportService> textExportServices)
{
    _textExportService = textExportServices;    
}

public TextExportService(IHotFolderManager hotFolder)
{
    _hotFolder = hotFolder;
}

public HotFolderManager(string baseFolderPath, string defaultFileSearchPattern)
{
    //Some IO
}

//In a Ninject AppModule...
Kernel.Bind<IList<ITextExportService>>().ToMethod(ctx => 
{ 
    var services = new List<ITextExportService>();

    foreach (var device in GetDevicesByEnvironment())
    {
        var service = ctx.Kernel.Get<ITextExportService>(new ConstructorArgument("hotFolder", ctx.Kernel.Get<IHotFolderManager>(
                                                                    new ConstructorArgument("baseFolderPath", device.Path),
                                                                    new ConstructorArgument("defaultFileSearchPattern", "*.jmf"))));

        services.Add(service);
    }
    return services;
});

I suspect this all stems from the fact that I don't explicitly have a binding registered for ITextExportService itself. But since the implementation is going to be dependent on data from App.config, I can't understand how I could register it and not just receive the same instance every time when I ask for a list of that type.


Solution

  • Related: Ninject different behaviour between Kernel.Get and Constructor Injection --> there's a mismatch with how ninject behaves when doing kernel.Get<IList<T>> and resolving an IList<T> parameter of a ctor.

    So here it goes:

    ninject's multi injection feature takes priority here. Whenever ninject encounters a request for IEnumerable<T>, IList<T> or T[] (but AFAIR not ICollection<T>) it will translate it to a request to resolve all bindings (without condition or with a matching condition) for T.

    Try to the following:

    public interface INoMultiBindingList<T> : IList<T> { }
    
    public class NoMultiBindingList<T> : List<T>, INoMultiBindingList<T> { }
    

    with:

    sbb public ServiceSchedulerBuilder(INoMultiBindingList textExportServices) { textExportService = textExportServices;
    }

    Kernel.Bind<INoMultiBindingList<ITextExportService>>().ToMethod(ctx => 
    { 
        var services = new NoMultiBindingList<ITextExportService>();
    
        foreach (var device in GetDevicesByEnvironment())
        {
            var service = ctx.Kernel.Get<ITextExportService>(new ConstructorArgument("hotFolder", ctx.Kernel.Get<IHotFolderManager>(
                                                                        new ConstructorArgument("baseFolderPath", device.Path),
                                                                        new ConstructorArgument("defaultFileSearchPattern", "*.jmf"))));
    
            services.Add(service);
        }
        return services;
    });