Skip to content

Child kernel bindings not being used to resolve dependencies #7

@pedroreys

Description

@pedroreys

I'm creating a child kernel and overriding the binding of a specific interface. My expectation is that all dependency resolutions using the child kernel will use this new binding. But, that's not what I'm seeing. The parent kernel binding is being used instead.

Here is a test that reproduces the issue:

public interface IA
{
    string GetValue();
}

public class A : IA
{
    private IB _b;

    public A(IB b)
    {
        _b = b;
    }

    public string GetValue()
    {
        return _b.Value;
    }
}

public interface IB
{
    string Value { get; set; }
}
public class B : IB
{
    public string Value { get; set; }
}


 public class TestRepro
  {
   [TestMethod]
    public void Should_not_use_Parents_Bindings()
    {
        var parentKernel = new StandardKernel();

        parentKernel.Bind<IA>().To<A>();
        parentKernel.Bind<IB>().To<B>();

        var childKernel = new ChildKernel(parentKernel);

        var otherB = new B() { Value = "newValue" };

        childKernel.Bind<IB>().ToConstant(otherB);

        childKernel.Get<IB>().Value.ShouldEqual(otherB.Value);

        childKernel.Get<IA>().GetValue().ShouldEqual(otherB.Value);

        // This last assertion fails. The value returned by IA.GetValue() is null
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions