Skip to content

Commit 535dfd7

Browse files
#138 Make anonymous roots lightweight
1 parent 5e8dab0 commit 535dfd7

6 files changed

Lines changed: 1292 additions & 47 deletions

File tree

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ public CompositionCode Build(DependencyGraph graph)
2727
break;
2828
}
2929

30-
if (root.Source.Kind.HasFlag(RootKinds.Light))
31-
{
32-
var lightweightRoot = root with
33-
{
34-
Lines = new Lines(),
35-
TypeDescription = typeResolver.Resolve(graph.Source, root.Injection.Type)
36-
};
37-
38-
roots.Add(lightweightRoot);
39-
continue;
40-
}
41-
4230
var lines = new Lines();
4331
using var rootToken = varsMap.Root(lines);
4432
var ctx = new RootContext(graph, root, varsMap, lines);
@@ -96,6 +84,11 @@ public CompositionCode Build(DependencyGraph graph)
9684
IsMethod = isMethod
9785
};
9886

87+
if (processedRoot.Kind.HasFlag(RootKinds.Light) && typeDescription.TypeArgs.Count > 0)
88+
{
89+
processedRoot = processedRoot with { Kind = processedRoot.Kind & ~RootKinds.Light };
90+
}
91+
9992
roots.Add(processedRoot);
10093
}
10194

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class LightweightRootClassBuilder(
88

99
public CompositionCode Build(CompositionCode composition)
1010
{
11-
var roots = composition.PublicRoots.Where(i => i.Kind.HasFlag(RootKinds.Light)).ToList();
11+
var roots = composition.PublicRoots
12+
.Where(i => i.Kind.HasFlag(RootKinds.Light))
13+
.ToList();
1214
if (roots.Count == 0)
1315
{
1416
return composition;
@@ -32,4 +34,4 @@ public CompositionCode Build(CompositionCode composition)
3234

3335
return composition with { MembersCount = membersCount };
3436
}
35-
}
37+
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,20 @@ private void BuildRoot(CompositionCode composition, Root root)
163163
else
164164
{
165165
Lines lines;
166-
if (root.Source.Kind.HasFlag(RootKinds.Light))
166+
if (root.Kind.HasFlag(RootKinds.Light))
167167
{
168168
lines = new Lines();
169-
lines.AppendLine($"return {Names.LightweightRootName}.{root.Source.UniqueName}();");
169+
var compositionTypeName = composition.Source.Source.Name.ClassName;
170+
var compositionInstance = root.IsStatic ? $"new {compositionTypeName}()." : string.Empty;
171+
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
172+
if (root.RootArgs.IsEmpty)
173+
{
174+
lines.AppendLine($"return {compositionInstance}{Names.LightweightRootName}.{root.Source.UniqueName}();");
175+
}
176+
else
177+
{
178+
lines.AppendLine($"return {compositionInstance}{Names.LightweightRootName}({string.Join(", ", root.RootArgs.Select(i => i.Name))}).{root.Source.UniqueName}();");
179+
}
170180
}
171181
else
172182
{
@@ -204,4 +214,4 @@ private void BuildRoot(CompositionCode composition, Root root)
204214
code.AppendLine("#pragma warning restore CS0162");
205215
}
206216
}
207-
}
217+
}

src/Pure.DI.Core/Core/DependencyGraphBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public IEnumerable<DependencyNode> Build(GraphBuildContext ctx)
5757
}
5858
else
5959
{
60-
if (node.Root.Source.Kind.HasFlag(RootKinds.Light))
60+
if (node.Root.Source.Kind.HasFlag(RootKinds.Light)
61+
&& node.Root.Source.RootType is not INamedTypeSymbol { IsGenericType: true })
6162
{
6263
processed.Add(processingNode);
6364
}
@@ -562,4 +563,4 @@ private IProcessingNode CreateNewProcessingNode(ICache<ProcessingNodeKey, IProce
562563

563564
private static object? GetContextTag(Injection injection, DependencyNode node) =>
564565
node.Factory is { Source.HasContextTag: true } ? injection.Tag : null;
565-
}
566+
}

src/Pure.DI.Core/Core/RootDependencyNodeBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public IEnumerable<DependencyNode> Build(DependencyNodeBuildContext ctx)
4141

4242
// ReSharper disable once InvertIf
4343
if (setup.Kind == CompositionKind.Public
44-
&& roots.Any(i => i.Kind.HasFlag(RootKinds.Light))
44+
&& roots.Any(i => i.Kind.HasFlag(RootKinds.Light)
45+
&& i.RootType is not INamedTypeSymbol { IsGenericType: true })
4546
&& types.TryGet(SpecialType.LightweightRoot, setup.SemanticModel.Compilation) is {} rootType)
4647
{
4748
var root = new MdRoot(
@@ -79,4 +80,4 @@ public IEnumerable<DependencyNode> Build(DependencyNodeBuildContext ctx)
7980
locationProvider));
8081
}
8182
}
82-
}
83+
}

0 commit comments

Comments
 (0)