Skip to content

Commit

Permalink
#74 Add the ability to build up an object
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 12, 2024
1 parent 50886c4 commit 9db79ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
4 changes: 2 additions & 2 deletions readme/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
32 changes: 26 additions & 6 deletions src/Pure.DI.Core/Core/InitializersWalker.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
namespace Pure.DI.Core;
// ReSharper disable ArrangeObjectCreationWhenTypeNotEvident
namespace Pure.DI.Core;

internal sealed class InitializersWalker(
string variableName,
IEnumerator<Variable> variables,
IInjections injections): DependenciesWalker<BuildContext>
{
private readonly List<Variable> _variables = new();

private readonly List<Variable> _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 ?? int.MaxValue).Select(i => i.Run))
{
action();
}
}

public override void VisitInjection(in BuildContext ctx, in Injection injection, bool hasExplicitDefaultValue, object? explicitDefaultValue, in ImmutableArray<Location> locations)
{
if (variables.MoveNext())
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion tests/Pure.DI.UsageTests/Basics/InitializationScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9db79ea

Please sign in to comment.