Skip to content

Commit

Permalink
The Bind method without parameters should only add bindings to first-…
Browse files Browse the repository at this point in the history
…level abstractions for the type
  • Loading branch information
NikolayPianikov committed Apr 18, 2024
1 parent 07f0efc commit 8d6a76a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/BaseSymbolsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public IEnumerable<ITypeSymbol> GetBaseSymbols(ITypeSymbol symbol, int deep = in
while (true)
{
yield return symbol;
foreach (var type in symbol.AllInterfaces.SelectMany(i => GetBaseSymbols(i, deep)))
foreach (var type in symbol.Interfaces.SelectMany(i => GetBaseSymbols(i, deep)))
{
yield return type;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Pure.DI.Core/Core/Code/VariablesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public Block Build(
var stack = new Stack<IStatement>(currentBlock.Statements);
while (stack.TryPop(out var currentStatement))
{
if (stack.Count > 0xffff)
{
throw new CompileErrorException($"Cyclic dependency has been found.", rootNode.Binding.Source.GetLocation(), LogId.ErrorCyclicDependency);
}

switch (currentStatement)
{
case Block block:
Expand Down
81 changes: 81 additions & 0 deletions tests/Pure.DI.IntegrationTests/GenericRootsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,85 @@ public static void Main()
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["2"], result);
}

[Fact]
public async Task ShouldSupportGenericRootWithTags()
{
// Given

// When
var result = await """
using System;
using System.Linq;
using Pure.DI;
using static Pure.DI.Lifetime;
namespace Sample
{
interface IRunner<T>
{
T Run(T value);
}
interface IPipeline<T> : IRunner<T>
{
}
class Pipeline<T> : IPipeline<T>
{
private readonly IRunner<T>[] _runners;
public Pipeline(params IRunner<T>[] runners)
{
_runners = runners;
}
public T Run(T value) =>
_runners.Aggregate(
value,
(current, handler) => handler.Run(current));
}
class Tracer<T>: IRunner<T>
{
public T Run(T value)
{
Console.WriteLine(value);
return value;
}
}
class SquareRootCalculator: IRunner<decimal>
{
public decimal Run(decimal value) =>
value * value;
}
internal partial class Composition
{
void Setup()
{
DI.Setup(nameof(Composition))
.Bind(0).To<Tracer<TT>>()
.Bind(1).To<SquareRootCalculator>()
.Bind(2).To<Tracer<TT>>()
.Bind().To<Pipeline<TT>>()
.Root<Pipeline<decimal>>("Pipeline");
}
}
public class Program
{
public static void Main()
{
new Composition().Pipeline.Run(7);
}
}
}
""".RunAsync(new Options(LanguageVersion.CSharp9));

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["7", "49"], result);
}
}

0 comments on commit 8d6a76a

Please sign in to comment.