From f5eebfaf7b3b1ff49a8a86dc0e48b9e86079cdf7 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Tue, 21 May 2024 16:59:32 +0300 Subject: [PATCH] Fixed wrong sequence of binding override in case of lambdas --- .../Core/VariationalDependencyGraphBuilder.cs | 4 +- tests/Pure.DI.IntegrationTests/SetupTests.cs | 72 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/Pure.DI.Core/Core/VariationalDependencyGraphBuilder.cs b/src/Pure.DI.Core/Core/VariationalDependencyGraphBuilder.cs index 65f04de7a..a6b953d69 100644 --- a/src/Pure.DI.Core/Core/VariationalDependencyGraphBuilder.cs +++ b/src/Pure.DI.Core/Core/VariationalDependencyGraphBuilder.cs @@ -133,7 +133,9 @@ private static IEnumerable CreateVariants(IEnumerable private static IEnumerable SortByPriority(IEnumerable nodes) => nodes.GroupBy(i => i.Binding) - .SelectMany(grp => grp.OrderBy(i => i.Implementation?.Constructor.Ordinal ?? int.MaxValue) + .OrderBy(i => i.Key.Id) + .SelectMany(grp => grp + .OrderBy(i => i.Implementation?.Constructor.Ordinal ?? int.MaxValue) .ThenByDescending(i => i.Implementation?.Constructor.Parameters.Count(p => !p.ParameterSymbol.IsOptional)) .ThenByDescending(i => i.Implementation?.Constructor.Method.DeclaredAccessibility)); } \ No newline at end of file diff --git a/tests/Pure.DI.IntegrationTests/SetupTests.cs b/tests/Pure.DI.IntegrationTests/SetupTests.cs index 89eb3f458..597b536f3 100644 --- a/tests/Pure.DI.IntegrationTests/SetupTests.cs +++ b/tests/Pure.DI.IntegrationTests/SetupTests.cs @@ -1759,4 +1759,76 @@ public static void Main() result.StdOut.ShouldBe(["(1, 2)"], result); } #endif + + [Fact] + public async Task ShouldOverrideBindingWithSeveralInterfaces() + { + // Given + + // When + var result = await """ +using System; +using Pure.DI; + +namespace Sample +{ + interface IDependency + { + void Do(); + } + + interface IAbc + { + } + + interface IXyz + { + } + + class Dependency : IDependency, IAbc + { + public void Do() + { + } + } + + class MyDependency : IDependency, IXyz + { + public void Do() + { + } + } + + static class Setup + { + private static void SetupComposition() + { + DI.Setup("Composition") + .Bind().To() + .Bind().To(_ => new MyDependency()) + .Root("Dep") + .Root("Abc") + .Root("Xyz"); + } + } + + public class Program + { + public static void Main() + { + var composition = new Composition(); + Console.WriteLine(composition.Dep); + Console.WriteLine(composition.Abc); + Console.WriteLine(composition.Xyz); + } + } +} +""".RunAsync(); + + // Then + result.Success.ShouldBeFalse(result); + result.StdOut.ShouldBe(["Sample.MyDependency", "Sample.Dependency", "Sample.MyDependency"], result); + result.Warnings.Count.ShouldBe(1); + result.Warnings.Count(i => i.Id == LogId.WarningOverriddenBinding).ShouldBe(1); + } } \ No newline at end of file