Skip to content

Commit

Permalink
Fixed the problem of duplicates in collections when overriding a binding
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Apr 18, 2024
1 parent 67f2ac3 commit 07f0efc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/Pure.DI.Core/Core/DependencyGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ private MdBinding CreateConstructBinding(MdSetup setup,
{
elementType = elementType.WithNullableAnnotation(NullableAnnotation.NotAnnotated);
var dependencyContracts = new List<MdContract>();
var contracts = new HashSet<Injection>();
foreach (var nestedBinding in setup.Bindings.Where(i => i != targetNode.Binding))
{
var matchedContracts = GetMatchedMdContracts(targetNode.Binding.SemanticModel.Compilation, elementType, nestedBinding).ToArray();
Expand All @@ -541,6 +542,24 @@ private MdBinding CreateConstructBinding(MdSetup setup,
.Concat(nestedBinding.Tags)
.Select((i, position) => i with { Position = position })
.ToImmutableArray();

var isDuplicate = false;
if (constructKind is MdConstructKind.Enumerable or MdConstructKind.Array or MdConstructKind.Span or MdConstructKind.AsyncEnumerable)
{
foreach (var mdTag in tags.DefaultIfEmpty(new MdTag(0, default)))
{
if (!contracts.Add(new Injection(elementType, mdTag)))
{
isDuplicate = true;
}
}
}

if (isDuplicate)
{
continue;
}

dependencyContracts.Add(new MdContract(targetNode.Binding.SemanticModel, targetNode.Binding.Source, elementType, tags));
}

Expand Down
11 changes: 6 additions & 5 deletions tests/Pure.DI.IntegrationTests/BclInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private static void SetupComposition()
{
// FormatCode = On
DI.Setup("Composition")
.Bind<IDependency>(1).To<Dependency>()
.Bind<IDependency>(1).To<Dependency>()
.Bind<IDependency>(2).To<Dependency>()
.Bind<IDependency>(3).To<Dependency>()
Expand Down Expand Up @@ -119,8 +120,8 @@ public static void Main()
});

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create("Dependency created", "Dependency created", "Dependency created", "Service creating"), result);
result.Errors.Count.ShouldBe(0, result);
result.StdOut.ShouldBe(["Dependency created", "Dependency created", "Dependency created", "Service creating"], result);
}

[Theory]
Expand Down Expand Up @@ -240,7 +241,7 @@ public static void Main()

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create("Dependency created", "Dependency created", "Dependency created", "Service creating"), result);
result.StdOut.ShouldBe(["Dependency created", "Dependency created", "Dependency created", "Service creating"], result);
}

[Theory]
Expand Down Expand Up @@ -356,7 +357,7 @@ public static void Main()

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create("Service creating"), result);
result.StdOut.ShouldBe(["Service creating"], result);
}

[Theory]
Expand Down Expand Up @@ -455,6 +456,6 @@ public static void Main()

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create("Dependency created", "Dependency created", "Dependency created", "Service creating"), result);
result.StdOut.ShouldBe(["Dependency created", "Dependency created", "Dependency created", "Service creating"], result);
}
}

0 comments on commit 07f0efc

Please sign in to comment.