Skip to content

Commit

Permalink
#84 Error “Sequence does not contain elements” when using BuildUp wit…
Browse files Browse the repository at this point in the history
…h non-transitive dependencies
  • Loading branch information
NikolayPianikov committed Jan 16, 2025
1 parent 06d7e4e commit 969fee5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/FactoryCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void Build(BuildContext ctx, in DpFactory factory)
.Zip(factory.Initializers, (initialization, initializer) => (initialization, initializer))
.GetEnumerator();

var initializationArgsEnum = initializationArgs.OfType<Variable>().GetEnumerator();
var initializationArgsEnum = initializationArgs.Select(i => i.Current).GetEnumerator();

var injectionsCtx = ctx;
if (variable.IsLazy && variable.Node.Accumulators.Count > 0)
Expand Down
74 changes: 74 additions & 0 deletions tests/Pure.DI.IntegrationTests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,4 +2078,78 @@ public static void Main()
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["Initialize Abc"], result);
}

[Theory]
[InlineData(Lifetime.Transient)]
[InlineData(Lifetime.PerBlock)]
[InlineData(Lifetime.PerResolve)]
[InlineData(Lifetime.Scoped)]
[InlineData(Lifetime.Singleton)]
internal async Task ShouldSupportBuildUpWhenLifetime(Lifetime lifetime)
{
// Given

// When
var result = await """
using System;
using Pure.DI;
namespace Sample
{
class ClientScript
{
[Ordinal(1)] public IService1? service1;
[Ordinal(2)] public IService2? service2;
}
interface IService1
{
};
internal class Service1 : IService1
{
};
interface IService2
{
};
internal class Service2 : IService2
{
};
static class Setup
{
private static void SetupComposition()
{
DI.Setup("Composition")
.Bind().To<Service1>()
.Bind().As(Lifetime.#lifetime#).To<Service2>()
.Bind().To<ClientScript>(ctx =>
{
ctx.Inject("from arg", out ClientScript script);
ctx.BuildUp(script);
return script;
})
.RootArg<ClientScript>("clientScript", "from arg")
.Root<ClientScript>("Initialize");
}
}
public class Program
{
public static void Main()
{
var composition = new Composition();
var script = new ClientScript();
script = composition.Initialize(script);
}
}
}
""".Replace("#lifetime#", lifetime.ToString()).RunAsync();

// Then
result.Errors.Count.ShouldBe(0, result);
}
}

0 comments on commit 969fee5

Please sign in to comment.