Skip to content

Error when injecting Func<IEnumerable<T>> #31

@johannesegger

Description

@johannesegger

(Not sure if this is a bug in Ninject itself or in the factory extension)

In the following example BusinessLogic gets the factories for MySqlDb and SqliteDb but not for MsSqlDb. MsSqlDb is bound using WhenInjectedInto<T>, but still it should work, shouldn't it?

public class NinjectTest
{
    [Fact]
    public void ShouldLazilyCreateObjects()
    {
        var kernel = new StandardKernel();
        kernel.Bind<BusinessLogic>().ToSelf();
        kernel.Bind<IDb>().To<MsSqlDb>().WhenInjectedInto<BusinessLogic>().InTransientScope();
        kernel.Bind<IDb>().To<MySqlDb>().InTransientScope();
        kernel.Bind<IDb>().To<SqliteDb>().InTransientScope();
        var bl = kernel.Get<BusinessLogic>();
        Assert.Equal(3, bl.DbFactories().Count());
    }
}

public interface IDb { }
public class SqliteDb : IDb { }
public class MySqlDb : IDb { }
public class MsSqlDb : IDb {  }

public class BusinessLogic
{
    public IEnumerable<Func<IDb>> DbFactories { get; }

    public BusinessLogic(IEnumerable<Func<IDb>> dbFactories)
    {
        DbFactories = dbFactories;
    }
}

I also tried injecting it as IEnumerable<Func<IDb>> but that gets me only one Func and invoking that fails with an ActivationException that there are too many bindings:

An exception of type 'Ninject.ActivationException' occurred in Ninject.dll but was not handled in user code

Additional information: Error activating IDb

More than one matching bindings are available.

Matching bindings:

  1) binding from IDb to MySqlDb

  2) binding from IDb to SqliteDb

Activation path:

  1) Request for IDb



Suggestions:

  1) Ensure that you have defined a binding for IDb only once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions