Skip to content

Commit 8b98492

Browse files
6
1 parent c3647bf commit 8b98492

7 files changed

Lines changed: 13 additions & 31 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public CompositionCode Build(CompositionCode composition)
6161

6262
if (hasStateObjectScope && hasScopedLifetimes)
6363
{
64-
code.AppendLine($"{Names.RootScopeFieldName} = new ScopeHandle(this, isRootScope: true);");
64+
code.AppendLine($"{Names.RootScopeFieldName} = new {Names.ScopeClassName}(this, isRootScope: true);");
6565
}
6666
}
6767

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public CompositionCode Build(CompositionCode composition)
3737

3838
if (hasStateObjectScope && hasScopedLifetimes)
3939
{
40-
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] private readonly ScopeHandle {Names.RootScopeFieldName};");
40+
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] private readonly {Names.ScopeClassName} {Names.RootScopeFieldName};");
4141
membersCounter++;
4242

4343
code.AppendLine("[ThreadStatic]");
44-
code.AppendLine($"private static ScopeHandle{nullable} {Names.CurrentScopeFieldName};");
44+
code.AppendLine($"private static {Names.ScopeClassName}{nullable} {Names.CurrentScopeFieldName};");
4545
membersCounter++;
4646
}
4747

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public CompositionCode Build(CompositionCode composition)
9191

9292
if (hasStateObjectScope && hasScopedLifetimes)
9393
{
94-
code.AppendLine($"{Names.RootScopeFieldName} = new ScopeHandle(this, isRootScope: true);");
94+
code.AppendLine($"{Names.RootScopeFieldName} = new {Names.ScopeClassName}(this, isRootScope: true);");
9595
}
9696
}
9797

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ private CompositionCode BuildStateObjectScopeFactory(CompositionCode composition
3838
code.AppendLine("/// </summary>");
3939
}
4040

41-
code.AppendLine("public ScopeHandle CreateScope()");
41+
code.AppendLine($"public {Names.ScopeClassName} CreateScope()");
4242
using (code.CreateBlock())
4343
{
44-
code.AppendLine("return new ScopeHandle(this, isRootScope: false);");
44+
code.AppendLine($"return new {Names.ScopeClassName}(this, isRootScope: false);");
4545
}
4646

4747
membersCounter++;

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,12 @@ public CompositionCode Build(CompositionCode composition)
4242
var interfaces = hasAsyncDisposables
4343
? $"{Names.IDisposableTypeName}, {Names.IAsyncDisposableTypeName}"
4444
: Names.IDisposableTypeName;
45-
code.AppendLine($"public sealed class ScopeHandle: {interfaces}");
45+
code.AppendLine($"public sealed class {Names.ScopeClassName}: {interfaces}");
4646
using (code.CreateBlock())
4747
{
4848
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] private readonly {compositionTypeName} _composition;");
49-
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] private readonly ScopeHandle{nullable} _previousScope;");
49+
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] private readonly {Names.ScopeClassName}{nullable} _previousScope;");
5050
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] private readonly bool _isRootScope;");
51-
code.AppendLine(new Line(int.MinValue, "#if NET9_0_OR_GREATER"));
52-
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] internal readonly {Names.LockTypeName} {Names.LockFieldName};");
53-
code.AppendLine(new Line(int.MinValue, "#else"));
54-
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] internal readonly {Names.ObjectTypeName} {Names.LockFieldName};");
55-
code.AppendLine(new Line(int.MinValue, "#endif"));
5651
code.AppendLine($"[{Names.NonSerializedAttributeTypeName}] private bool _isDisposed;");
5752
if (hasScopedDisposables)
5853
{
@@ -79,18 +74,13 @@ public CompositionCode Build(CompositionCode composition)
7974
}
8075

8176
code.AppendLine();
82-
code.AppendLine($"internal ScopeHandle({compositionTypeName} composition, bool isRootScope)");
77+
code.AppendLine($"internal {Names.ScopeClassName}({compositionTypeName} composition, bool isRootScope)");
8378
using (code.CreateBlock())
8479
{
8580
const string compositionLocalName = "compositionChecked";
8681
code.AppendLine($"var {compositionLocalName} = composition ?? throw new {Names.SystemNamespace}ArgumentNullException(nameof(composition));");
8782
code.AppendLine("_composition = compositionChecked;");
8883
code.AppendLine("_isRootScope = isRootScope;");
89-
code.AppendLine(new Line(int.MinValue, "#if NET9_0_OR_GREATER"));
90-
code.AppendLine($"{Names.LockFieldName} = new {Names.LockTypeName}();");
91-
code.AppendLine(new Line(int.MinValue, "#else"));
92-
code.AppendLine($"{Names.LockFieldName} = new {Names.ObjectTypeName}();");
93-
code.AppendLine(new Line(int.MinValue, "#endif"));
9484
if (hasScopedDisposables)
9585
{
9686
code.AppendLine($"{Names.DisposablesFieldName} = new object[{composition.DisposablesScopedCount.ToString()}];");
@@ -122,7 +112,7 @@ public CompositionCode Build(CompositionCode composition)
122112
code.AppendLine();
123113
}
124114

125-
code.AppendLine($"lock ({Names.LockFieldName})");
115+
code.AppendLine($"lock (_composition.{Names.LockFieldName})");
126116
using (code.CreateBlock())
127117
{
128118
code.AppendLine("if (_isDisposed)");
@@ -199,7 +189,7 @@ public CompositionCode Build(CompositionCode composition)
199189
code.AppendLine();
200190
}
201191

202-
code.AppendLine($"lock ({Names.LockFieldName})");
192+
code.AppendLine($"lock (_composition.{Names.LockFieldName})");
203193
using (code.CreateBlock())
204194
{
205195
code.AppendLine("if (_isDisposed)");

src/Pure.DI.Core/Core/Code/RootCodeBuilder.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,7 @@ private void StartSingleInstanceCheck(CodeContext ctx)
153153
if (isLockRequired)
154154
{
155155
lines.IncIndent();
156-
if (var.AbstractNode.ActualLifetime == Scoped
157-
&& ctx.RootContext.Graph.Source.Hints.ScopeStrategy == ScopeStrategy.StateObject)
158-
{
159-
lines.AppendLine($"lock ({Names.ScopeVariableName}.{Names.LockFieldName})");
160-
}
161-
else
162-
{
163-
locks.AddLockStatements(ctx.RootContext.Root.IsStatic, lines, false);
164-
}
165-
156+
locks.AddLockStatements(ctx.RootContext.Root.IsStatic, lines, false);
166157
lines.IncIndent();
167158
lines.AppendLine($"if ({checkExpression})");
168159
}

src/Pure.DI.Core/Core/Names.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static class Names
3232
public const string DefaultBuilderName = "BuildUp";
3333
public const string BuildingInstance = "buildingInstance";
3434
public const string ContextInstance = "ctx";
35+
public const string ScopeClassName = "Scope";
3536
public const string DefaultInstanceValueName = "instance_1182D127";
3637
public static readonly string PrivateRootName = $"Root{Salt}";
3738
public const string TempInstanceValueNameSuffix = "Temp";

0 commit comments

Comments
 (0)