Skip to content

Commit 5312261

Browse files
Refactoring
1 parent 4c63a07 commit 5312261

13 files changed

Lines changed: 63 additions & 41 deletions

File tree

samples/AvaloniaApp/AvaloniaApp.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
<ProjectReference Include="..\..\src\Pure.DI.Core\Pure.DI.Core.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
1414
<ProjectReference Include="..\..\src\Pure.DI\Pure.DI.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
1515
<ProjectReference Include="..\Clock\Clock.csproj"/>
16-
<PackageReference Include="Avalonia" Version="11.3.4" />
17-
<PackageReference Include="Avalonia.Desktop" Version="11.3.4" />
18-
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.4" />
19-
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.4" />
16+
<PackageReference Include="Avalonia" Version="11.3.5" />
17+
<PackageReference Include="Avalonia.Desktop" Version="11.3.5" />
18+
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.5" />
19+
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.5" />
2020
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
21-
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.4" />
21+
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.5" />
2222
</ItemGroup>
2323

2424
<ItemGroup>

samples/AvaloniaSimpleApp/AvaloniaSimpleApp.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
<ItemGroup>
1111
<ProjectReference Include="..\..\src\Pure.DI.Core\Pure.DI.Core.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
1212
<ProjectReference Include="..\..\src\Pure.DI\Pure.DI.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
13-
<PackageReference Include="Avalonia" Version="11.3.4" />
14-
<PackageReference Include="Avalonia.Desktop" Version="11.3.4" />
15-
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.4" />
16-
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.4" />
13+
<PackageReference Include="Avalonia" Version="11.3.5" />
14+
<PackageReference Include="Avalonia.Desktop" Version="11.3.5" />
15+
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.5" />
16+
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.5" />
1717
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
18-
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.4" />
18+
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.5" />
1919
</ItemGroup>
2020
</Project>

samples/SingleRootAvaloniaApp/SingleRootAvaloniaApp.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
<ProjectReference Include="..\..\src\Pure.DI.Core\Pure.DI.Core.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
1515
<ProjectReference Include="..\..\src\Pure.DI\Pure.DI.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
1616
<ProjectReference Include="..\Clock\Clock.csproj"/>
17-
<PackageReference Include="Avalonia" Version="11.3.4" />
18-
<PackageReference Include="Avalonia.Desktop" Version="11.3.4" />
19-
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.4" />
20-
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.4" />
17+
<PackageReference Include="Avalonia" Version="11.3.5" />
18+
<PackageReference Include="Avalonia.Desktop" Version="11.3.5" />
19+
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.5" />
20+
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.5" />
2121
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
22-
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.4" />
22+
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.3.5" />
2323
</ItemGroup>
2424

2525
</Project>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Pure.DI.Core.Code;
22

33
class Accumulators(
4-
INodeInfo nodeInfo,
4+
INodeTools nodeTools,
55
IBuildTools buildTools)
66
: IAccumulators
77
{
@@ -27,7 +27,7 @@ class Accumulators(
2727
foreach (var dependency in dependencies)
2828
{
2929
var source = dependency.Source;
30-
if (nodeInfo.IsLazy(source.Node))
30+
if (nodeTools.IsLazy(source.Node))
3131
{
3232
continue;
3333
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class CompositionBuilder(
55
ITypeResolver typeResolver,
66
Func<IVarsMap> varsMapFactory,
77
Func<IBuilder<RootContext, VarInjection>> rootBuilder,
8-
INodeInfo nodeInfo,
8+
INodeTools nodeTools,
99
IVarDeclarationTools varDeclarationTools,
1010
IBuilder<CompositionCode, LinesBuilder> classDiagramBuilder,
1111
CancellationToken cancellationToken)
@@ -65,9 +65,9 @@ public CompositionCode Build(DependencyGraph graph)
6565
.ThenBy(root => root.DisplayName)
6666
.ToImmutableArray();
6767

68-
var totalDisposables = singletons.Where(i => nodeInfo.IsDisposableAny(i.Node.Node)).ToList();
69-
var disposables = singletons.Where(i => nodeInfo.IsDisposable(i.Node.Node)).ToList();
70-
var asyncDisposables = singletons.Where(i => nodeInfo.IsAsyncDisposable(i.Node.Node)).ToList();
68+
var totalDisposables = singletons.Where(i => nodeTools.IsDisposableAny(i.Node.Node)).ToList();
69+
var disposables = singletons.Where(i => nodeTools.IsDisposable(i.Node.Node)).ToList();
70+
var asyncDisposables = singletons.Where(i => nodeTools.IsAsyncDisposable(i.Node.Node)).ToList();
7171
var composition = new CompositionCode(
7272
graph,
7373
new LinesBuilder(),
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
namespace Pure.DI.Core.Code;
22

3-
interface INodeInfo
3+
interface INodeTools
44
{
55
bool IsLazy(DependencyNode node);
66

7+
bool IsBlock(IDependencyNode node);
8+
79
bool IsDisposableAny(DependencyNode node);
810

911
bool IsDisposable(DependencyNode node);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Pure.DI.Core.Code;
2+
3+
interface ILocalFunctions
4+
{
5+
bool UseFor(CodeContext ctx);
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Pure.DI.Core.Code;
2+
3+
class LocalFunctions(INodeTools nodeTools): ILocalFunctions
4+
{
5+
public bool UseFor(CodeContext ctx)
6+
{
7+
var var = ctx.VarInjection.Var;
8+
return ctx is { HasOverrides: false, Accumulators.Length: 0 }
9+
&& nodeTools.IsBlock(var.AbstractNode)
10+
&& ctx.RootContext.Graph.Graph.TryGetOutEdges(var.Declaration.Node.Node, out var targets)
11+
&& targets.Count > 1;
12+
}
13+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Pure.DI.Core.Code;
44

5+
using static Lifetime;
56
using SpecialType=Microsoft.CodeAnalysis.SpecialType;
67

7-
sealed class NodeInfo(ITypes types) : INodeInfo
8+
sealed class NodeTools(ITypes types) : INodeTools
89
{
910
public bool IsLazy(DependencyNode node) =>
1011
IsDelegate(node) || IsEnumerable(node) || IsAsyncEnumerable(node);
1112

13+
public bool IsBlock(IDependencyNode node) =>
14+
node.Lifetime is Singleton or Scoped or PerResolve;
15+
1216
public bool IsDisposableAny(DependencyNode node) =>
1317
node.Type.AllInterfaces.Any(i =>
1418
i.SpecialType == SpecialType.System_IDisposable

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Pure.DI.Core.Code;
1010
using static LinesBuilderExtensions;
1111

1212
class RootBuilder(
13-
INodeInfo nodeInfo,
13+
INodeTools nodeTools,
1414
IBuildTools buildTools,
1515
IAccumulators accumulators,
1616
ILocks locks,
@@ -27,6 +27,7 @@ class RootBuilder(
2727
INameProvider nameProvider,
2828
IOverridesRegistry overridesRegistry,
2929
INameFormatter nameFormatter,
30+
ILocalFunctions localFunctions,
3031
CancellationToken cancellationToken)
3132
: IBuilder<RootContext, VarInjection>
3233
{
@@ -102,25 +103,21 @@ private void BuildCode(CodeContext parentCtx)
102103

103104
var varsMap = varCtx.VarsMap;
104105
var setup = varCtx.RootContext.Graph.Source;
105-
var isBlock = IsBlock(var.AbstractNode);
106-
var isLazy = nodeInfo.IsLazy(var.AbstractNode.Node);
106+
var isBlock = nodeTools.IsBlock(var.AbstractNode);
107+
var isLazy = nodeTools.IsLazy(var.AbstractNode.Node);
107108
var.HasCycle ??= IsCycle(varCtx.RootContext.Graph.Graph, var.Declaration.Node.Node, ImmutableHashSet<DependencyNode>.Empty);
108109
var acc = isLazy ? accumulators.GetAccumulators(varCtx.RootContext.Graph.Graph, var.AbstractNode).ToImmutableArray() : ImmutableArray<(MdAccumulator, Dependency)>.Empty;
109-
var useLocalFunction = isBlock
110-
&& !varCtx.HasOverrides
111-
&& varCtx.Accumulators.Length == 0
112-
&& varCtx.RootContext.Graph.Graph.TryGetOutEdges(var.Declaration.Node.Node, out var targets) && targets.Count > 1;
113-
110+
var isLocalFunction = localFunctions.UseFor(varCtx);
114111
var mapToken =
115-
useLocalFunction
112+
isLocalFunction
116113
? varsMap.LocalFunction(var, lines)
117114
: isLazy
118115
? varsMap.Lazy(var, lines)
119116
: isBlock
120117
? varsMap.Block(var, lines)
121118
: Disposables.Empty;
122119

123-
if (useLocalFunction || isLazy)
120+
if (isLocalFunction || isLazy)
124121
{
125122
varCtx = varCtx with { IsLockRequired = varCtx.RootContext.IsThreadSafeEnabled };
126123
}
@@ -299,7 +296,7 @@ private void BuildCode(CodeContext parentCtx)
299296
{
300297
ExpressionSyntax? value = null;
301298
var type = memberResolver.ContractType;
302-
ExpressionSyntax instance = member.IsStatic
299+
var instance = member.IsStatic
303300
? SyntaxFactory.ParseTypeName(symbolNames.GetGlobalName(type))
304301
: SyntaxFactory.IdentifierName(Names.DefaultInstanceValueName);
305302

@@ -735,7 +732,7 @@ private void BuildCode(CodeContext parentCtx)
735732

736733
mapToken.Dispose();
737734

738-
if (useLocalFunction)
735+
if (isLocalFunction)
739736
{
740737
var baseName = nameFormatter.Format("{type}{tag}", varCtx.VarInjection.Var.InstanceType, varCtx.VarInjection.Injection.Tag);
741738
var localFunction = var.LocalFunction;
@@ -811,8 +808,6 @@ private static int GetInjectionPriority(VarInjection varInjection)
811808
return int.MaxValue;
812809
}
813810

814-
private static bool IsBlock(IDependencyNode node) => node.Lifetime is Singleton or Scoped or PerResolve;
815-
816811
private void StartSingleInstanceCheck(CodeContext ctx)
817812
{
818813
var isLockRequired = ctx.IsLockRequired;
@@ -840,7 +835,7 @@ private void FinishSingleInstanceCheck(CodeContext ctx)
840835
{
841836
var var = ctx.VarInjection.Var;
842837
var lines = ctx.Lines;
843-
if (var.AbstractNode.Lifetime is Singleton or Scoped && nodeInfo.IsDisposableAny(var.AbstractNode.Node))
838+
if (var.AbstractNode.Lifetime is Singleton or Scoped && nodeTools.IsDisposableAny(var.AbstractNode.Node))
844839
{
845840
var parent = "";
846841
if (var.AbstractNode.Lifetime == Singleton)

0 commit comments

Comments
 (0)