Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johannesegger opened this issue Oct 6, 2015 · 2 comments
Open

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

johannesegger opened this issue Oct 6, 2015 · 2 comments

Comments

@johannesegger
Copy link

(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.
@scott-xu
Copy link
Member

scott-xu commented Oct 6, 2015

MsSqlDb's parent is Func but not BusinessLogic

@johannesegger
Copy link
Author

I see. How would I then inject it into BusinessLogic?
Although it wouldn't solve my problem, I tried WhenInjectedInto<Func<IDb>> but that didn't work either. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants