Skip to content

Mulitple binding (as attributes) sometimes fail to be properly resolved in a single dependency graph #124

Description

@W-Sebastian

Describe the bug
I changed the BindAttributeScenarios.cs from UsageTests to the following:

namespace Pure.DI.UsageTests.Basics.BindAttributeScenario;

using Xunit;

// {
//# using Pure.DI;
// }

public class Scenario
{
    [Fact]
    public void Run()
    {
        // This hint indicates to not generate methods such as Resolve
        // Resolve = Off
// {
        DI.Setup(nameof(Composition))
            .Bind().As(Lifetime.Singleton).To<Facade>()
            .Bind().To<Service>()

            // Composition root
            .Root<IService>("Root");

        var composition = new Composition();
        var service = composition.Root;
        service.DoSomething();
// }
        composition.SaveClassDiagram();
    }
}

// {


interface IAnotherDependency
{
    public void DoSomethingElse();
}

public class AnotherDependency : IAnotherDependency
{
    public void DoSomethingElse()
    {
    }
}

interface IDependency
{
    public void DoSomething();
}

class Dependency : IDependency
{
    public void DoSomething()
    {
    }
}

class Facade
{
    [Bind]
    public IDependency Dependency { get; } = new Dependency();
    
    [Bind]
    public IAnotherDependency SecondDependency { get; } = new AnotherDependency();
}

interface IService
{
    public void DoSomething();
}

class Service(IDependency dep, IAnotherDependency anotherDep) : IService
{
    public void DoSomething() => dep.DoSomething();
}

This results in:

4>BindAttributeScenario.cs(86,27): Error DIE000 : Unable to resolve "Pure.DI.UsageTests.Basics.BindAttributeScenario.IDependency" in Service(Pure.DI.UsageTests.Basics.BindAttributeScenario.IDependency dep<--Pure.DI.UsageTests.Basics.BindAttributeScenario.IDependency), Pure.DI.UsageTests.Basics.BindAttributeScenario.IAnotherDependency anotherDep<--Pure.DI.UsageTests.Basics.BindAttributeScenario.IAnotherDependency)).
4>BindAttributeScenario.cs(86,51): Error CS9113 : Parameter 'anotherDep' is unread.
4>BindAttributeScenario.cs(30,25): Error CS0103 : The name 'Composition' does not exist in the current context
4>BindAttributeScenario.cs(37,31): Error CS0246 : The type or namespace name 'Composition' could not be found (are you missing a using directive or an assembly reference?)

The two bindings provided by the Facade fail to provide anymore.

To Reproduce
Update the BindAttributeScenario to the provided code. Additionally introduce a new unit test in BindAttributeTests that is similar with the provided code and observe the failure.

Expected behavior
Both dependencies should be binded and provided when needed.

Additional context
There is an (ugly) workaround where instead of using Bind attribute we just expose them manually in the factory.
I am also trying to debug the issue but not being familiar with the code base might take a while. If I have any findings I will add them here.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions