From 81ef10256c9c9031176842d05307c006883d7688 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Tue, 12 Nov 2024 16:48:43 +0300 Subject: [PATCH] #74 Add the ability to build up an object --- readme/initialization.md | 4 +-- src/Pure.DI.Core/Core/InitializersWalker.cs | 32 +++++++++++++++---- .../Basics/InitializationScenario.cs | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/readme/initialization.md b/readme/initialization.md index 9032f5c6..3f88e6a0 100644 --- a/readme/initialization.md +++ b/readme/initialization.md @@ -16,7 +16,7 @@ interface IDependency class Dependency : IDependency { // The Ordinal attribute specifies to perform an injection and its order - [Ordinal(3)] + [Ordinal(1)] public string Name { get; set; } = ""; public Guid Id { get; private set; } = Guid.Empty; @@ -79,8 +79,8 @@ partial class Composition Guid transientGuid2 = Guid.NewGuid(); Dependency transientDependency1; var localDependency28= new Dependency(); - localDependency28.Name = name; localDependency28.SetId(transientGuid2); + localDependency28.Name = name; transientDependency1 = localDependency28; return new Service(transientDependency1); } diff --git a/src/Pure.DI.Core/Core/InitializersWalker.cs b/src/Pure.DI.Core/Core/InitializersWalker.cs index 779aca8d..3ad11cea 100644 --- a/src/Pure.DI.Core/Core/InitializersWalker.cs +++ b/src/Pure.DI.Core/Core/InitializersWalker.cs @@ -1,12 +1,23 @@ -namespace Pure.DI.Core; +// ReSharper disable ArrangeObjectCreationWhenTypeNotEvident +namespace Pure.DI.Core; internal sealed class InitializersWalker( string variableName, IEnumerator variables, IInjections injections): DependenciesWalker { - private readonly List _variables = new(); - + private readonly List _variables = []; + private readonly List<(Action Run, int? Ordinal)> _actions = []; + + public override void VisitInitializer(in BuildContext ctx, DpInitializer initializer) + { + base.VisitInitializer(in ctx, initializer); + foreach (var action in _actions.OrderBy(i => i.Ordinal ?? 0).Select(i => i.Run)) + { + action(); + } + } + public override void VisitInjection(in BuildContext ctx, in Injection injection, bool hasExplicitDefaultValue, object? explicitDefaultValue, in ImmutableArray locations) { if (variables.MoveNext()) @@ -20,21 +31,30 @@ public override void VisitInjection(in BuildContext ctx, in Injection injection, public override void VisitMethod(in BuildContext ctx, in DpMethod method) { base.VisitMethod(in ctx, in method); - injections.MethodInjection(variableName, ctx, method, _variables.ToImmutableArray()); + var curCtx = ctx; + var curMethod = method; + var curVariables = _variables.ToImmutableArray(); + _actions.Add(new (() => injections.MethodInjection(variableName, curCtx, curMethod, curVariables), curMethod.Ordinal)); _variables.Clear(); } public override void VisitProperty(in BuildContext ctx, in DpProperty property) { base.VisitProperty(in ctx, in property); - injections.PropertyInjection(variableName, ctx, property, _variables.Single()); + var curCtx = ctx; + var curProperty = property; + var curVariable = _variables.Single(); + _actions.Add(new (() => injections.PropertyInjection(variableName, curCtx, curProperty, curVariable), curProperty.Ordinal)); _variables.Clear(); } public override void VisitField(in BuildContext ctx, in DpField field) { base.VisitField(in ctx, in field); - injections.FieldInjection(variableName, ctx, field, _variables.Single()); + var curCtx = ctx; + var curField = field; + var curVariable = _variables.Single(); + _actions.Add(new (() => injections.FieldInjection(variableName, curCtx, curField, curVariable), curField.Ordinal)); _variables.Clear(); } } \ No newline at end of file diff --git a/tests/Pure.DI.UsageTests/Basics/InitializationScenario.cs b/tests/Pure.DI.UsageTests/Basics/InitializationScenario.cs index a5094e11..f800d21b 100644 --- a/tests/Pure.DI.UsageTests/Basics/InitializationScenario.cs +++ b/tests/Pure.DI.UsageTests/Basics/InitializationScenario.cs @@ -26,7 +26,7 @@ interface IDependency class Dependency : IDependency { // The Ordinal attribute specifies to perform an injection and its order - [Ordinal(3)] + [Ordinal(1)] public string Name { get; set; } = ""; public Guid Id { get; private set; } = Guid.Empty;