Skip to content

Commit 4d1cded

Browse files
Refactor ScopeConstructorBuilder to simplify composition logic
1 parent 2118334 commit 4d1cded

2 files changed

Lines changed: 12 additions & 147 deletions

File tree

src/Pure.DI.Core/Core/Code/Parts/ScopeConstructorBuilder.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,10 @@ public CompositionCode Build(CompositionCode composition)
1919

2020
var code = composition.Code;
2121
var membersCounter = composition.MembersCount;
22-
var hints = composition.Hints;
23-
var setupContextMembersToCopy = composition.SetupContextMembersToCopy;
24-
var classArgs = composition.ClassArgs.GetArgsOfKind(ArgKind.Composition).ToList();
25-
var setupContextArgsToCopy = composition.SetupContextArgsToCopy;
26-
var isLockRequired = composition.IsLockRequired;
27-
var ScopeMethodName = composition.ScopeMethodName;
28-
var isScopeMethod = composition.IsScopeMethod;
29-
var isCommentsEnabled = hints.IsCommentsEnabled;
3022
string source, destination;
31-
if (isScopeMethod)
23+
if (composition.IsScopeMethod)
3224
{
33-
if (isCommentsEnabled)
25+
if (composition.Hints.IsCommentsEnabled)
3426
{
3527
code.AppendLine("/// <summary>");
3628
code.AppendLine($"/// This method setups <paramref name=\"{Names.ChildScopeArgName}\"/> scope based on <paramref name=\"{Names.ParentScopeArgName}\"/>. This allows the <see cref=\"Lifetime.Scoped\"/> life time to be applied.");
@@ -41,11 +33,11 @@ public CompositionCode Build(CompositionCode composition)
4133

4234
source = $"{Names.ParentScopeArgName}.";
4335
destination = $"{Names.ChildScopeArgName}.";
44-
code.AppendLine($"internal static {composition.Name.ClassName} {ScopeMethodName}({composition.Name.ClassName} {Names.ParentScopeArgName}, {composition.Name.ClassName} {Names.ChildScopeArgName})");
36+
code.AppendLine($"internal static {composition.Name.ClassName} {composition.ScopeMethodName}({composition.Name.ClassName} {Names.ParentScopeArgName}, {composition.Name.ClassName} {Names.ChildScopeArgName})");
4537
}
4638
else
4739
{
48-
if (isCommentsEnabled)
40+
if (composition.Hints.IsCommentsEnabled)
4941
{
5042
code.AppendLine("/// <summary>");
5143
code.AppendLine($"/// This constructor creates a new instance of <see cref=\"{composition.Name.ClassName}\"/> scope based on <paramref name=\"{Names.ParentScopeArgName}\"/>. This allows the <see cref=\"Lifetime.Scoped\"/> life time to be applied.");
@@ -62,7 +54,7 @@ public CompositionCode Build(CompositionCode composition)
6254
using (code.CreateBlock())
6355
{
6456
code.AppendLine($"if ({Names.ObjectTypeName}.ReferenceEquals({Names.ParentScopeArgName}, null)) throw new {Names.SystemNamespace}ArgumentNullException(nameof({Names.ParentScopeArgName}));");
65-
if (isScopeMethod)
57+
if (composition.IsScopeMethod)
6658
{
6759
code.AppendLine($"if ({Names.ObjectTypeName}.ReferenceEquals({Names.ChildScopeArgName}, null)) throw new {Names.SystemNamespace}ArgumentNullException(nameof({Names.ChildScopeArgName}));");
6860
if (composition.Singletons.Length > 0)
@@ -78,35 +70,35 @@ public CompositionCode Build(CompositionCode composition)
7870
}
7971
}
8072

81-
if (!isScopeMethod)
73+
if (!composition.IsScopeMethod)
8274
{
83-
foreach (var fieldArg in classArgs)
75+
foreach (var fieldArg in composition.ClassArgs.GetArgsOfKind(ArgKind.Composition))
8476
{
8577
code.AppendLine($"{destination}{fieldArg.Name} = {source}{fieldArg.Name};");
8678
}
8779
}
8880

89-
foreach (var contextArg in setupContextArgsToCopy)
81+
foreach (var contextArg in composition.SetupContextArgsToCopy)
9082
{
9183
code.AppendLine($"{destination}{contextArg.Name} = {source}{contextArg.Name};");
9284
}
9385

94-
foreach (var memberName in setupContextMembersToCopy)
86+
foreach (var memberName in composition.SetupContextMembersToCopy)
9587
{
9688
code.AppendLine($"{destination}{memberName} = {source}{memberName};");
9789
}
9890

99-
if (isLockRequired)
91+
if (composition.IsLockRequired)
10092
{
10193
code.AppendLine($"{destination}{Names.LockFieldName} = {source}{Names.LockFieldName};");
10294
}
10395

104-
if (composition.DisposablesScopedCount > 0)
96+
if (composition.DisposablesScopedCount > 0 && constructors.IsEnabled(composition.Source))
10597
{
10698
code.AppendLine($"{destination}{Names.DisposablesFieldName} = new object[{composition.DisposablesScopedCount.ToString()}];");
10799
}
108100

109-
if (isScopeMethod)
101+
if (composition.IsScopeMethod)
110102
{
111103
code.AppendLine($"return {Names.ChildScopeArgName};");
112104
}

tests/Pure.DI.UsageTests/ScenarioMetadataTests.cs

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)