Skip to content

Commit

Permalink
Fixed wrong sequence of binding override in case of lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed May 21, 2024
1 parent c70367a commit f5eebfa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Pure.DI.Core/Core/VariationalDependencyGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ private static IEnumerable<Variation> CreateVariants(IEnumerable<ProcessingNode>

private static IEnumerable<DependencyNode> SortByPriority(IEnumerable<DependencyNode> 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));
}
72 changes: 72 additions & 0 deletions tests/Pure.DI.IntegrationTests/SetupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dependency>()
.Bind().To(_ => new MyDependency())
.Root<IDependency>("Dep")
.Root<IAbc>("Abc")
.Root<IXyz>("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);
}
}

0 comments on commit f5eebfa

Please sign in to comment.