Skip to content

Commit

Permalink
Reducing the amount of generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Apr 28, 2024
1 parent fd04153 commit 8180eba
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/Pure.DI.Core/Core/Code/ResolverClassesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public CompositionCode Build(CompositionCode composition)

var code = composition.Code;
code.AppendLine("#region Resolvers");
code.AppendLine($"private sealed class {Names.ResolverClassName}<T>: {Names.ResolverInterfaceName}<{composition.Source.Source.Name.ClassName}, T>");
code.AppendLine($"private class {Names.ResolverClassName}<T>: {Names.ResolverInterfaceName}<{composition.Source.Source.Name.ClassName}, T>");
code.AppendLine("{");
using (code.Indent())
{
code.AppendLine($"public static {Names.ResolverInterfaceName}<{composition.Source.Source.Name.ClassName}, T> {Names.ResolverPropertyName} = new {Names.ResolverClassName}<T>();");

code.AppendLine();
code.AppendLine($"public T {Names.ResolveMethodName}({composition.Source.Source.Name.ClassName} composite)");
code.AppendLine($"public virtual T {Names.ResolveMethodName}({composition.Source.Source.Name.ClassName} composite)");
code.AppendLine("{");
using (code.Indent())
{
Expand All @@ -32,7 +32,7 @@ public CompositionCode Build(CompositionCode composition)
code.AppendLine("}");

code.AppendLine();
code.AppendLine($"public T {Names.ResolveByTagMethodName}({composition.Source.Source.Name.ClassName} composite, object tag)");
code.AppendLine($"public virtual T {Names.ResolveByTagMethodName}({composition.Source.Source.Name.ClassName} composite, object tag)");
code.AppendLine("{");
using (code.Indent())
{
Expand All @@ -50,24 +50,24 @@ public CompositionCode Build(CompositionCode composition)
{
var resolverClassName = resolver.ClassName;
code.AppendLine();
var interfaceName = $"{Names.ResolverInterfaceName}<{composition.Source.Source.Name.ClassName}, {resolver.Type}>";
var interfaces = new List<string> { interfaceName };
var objectInterfaceName = "";
var baseTypeName = $"{Names.ResolverClassName}<{resolver.Type}>";
var baseTypes = new List<string> { baseTypeName };
var objectTypeName = "";
if (resolver.Type.IsValueType)
{
objectInterfaceName = $"{Names.ResolverInterfaceName}<{composition.Source.Source.Name.ClassName}, object>";
interfaces.Add(objectInterfaceName);
objectTypeName = $"{Names.ResolverInterfaceName}<{composition.Source.Source.Name.ClassName}, object>";
baseTypes.Add(objectTypeName);
}

code.AppendLine($"private sealed class {resolverClassName}: {string.Join(", ", interfaces)}");
code.AppendLine($"private sealed class {resolverClassName}: {string.Join(", ", baseTypes)}");
code.AppendLine("{");
using (code.Indent())
{
ImplementInterface(composition, resolver, code);

if (!string.IsNullOrWhiteSpace(objectInterfaceName))
if (!string.IsNullOrWhiteSpace(objectTypeName))
{
code.AppendLine($"object {objectInterfaceName}.{Names.ResolveMethodName}({composition.Source.Source.Name.ClassName} composition)");
code.AppendLine($"object {objectTypeName}.{Names.ResolveMethodName}({composition.Source.Source.Name.ClassName} composition)");
code.AppendLine("{");
using (code.Indent())
{
Expand All @@ -76,7 +76,7 @@ public CompositionCode Build(CompositionCode composition)
code.AppendLine("}");

code.AppendLine();
code.AppendLine($"object {objectInterfaceName}.{Names.ResolveByTagMethodName}({composition.Source.Source.Name.ClassName} composition, object tag)");
code.AppendLine($"object {objectTypeName}.{Names.ResolveByTagMethodName}({composition.Source.Source.Name.ClassName} composition, object tag)");
code.AppendLine("{");
using (code.Indent())
{
Expand All @@ -96,7 +96,7 @@ public CompositionCode Build(CompositionCode composition)
private static void ImplementInterface(CompositionCode composition, ResolverInfo resolver, LinesBuilder code)
{
var defaultRoot = resolver.Roots.SingleOrDefault(i => i.Injection.Tag is null);
code.AppendLine($"public {resolver.Type} {Names.ResolveMethodName}({composition.Source.Source.Name.ClassName} composition)");
code.AppendLine($"public override {resolver.Type} {Names.ResolveMethodName}({composition.Source.Source.Name.ClassName} composition)");
code.AppendLine("{");
using (code.Indent())
{
Expand All @@ -108,15 +108,15 @@ private static void ImplementInterface(CompositionCode composition, ResolverInfo
}
else
{
code.AppendLine($"throw new {Names.SystemNamespace}InvalidOperationException($\"{Names.CannotResolve} of type {resolver.Type}.\");");
code.AppendLine($"return base.{Names.ResolveMethodName}(composition);");
}
}

code.AppendLine("}");

code.AppendLine();

code.AppendLine($"public {resolver.Type} {Names.ResolveByTagMethodName}({composition.Source.Source.Name.ClassName} composition, object tag)");
code.AppendLine($"public override {resolver.Type} {Names.ResolveByTagMethodName}({composition.Source.Source.Name.ClassName} composition, object tag)");
code.AppendLine("{");
using (code.Indent())
{
Expand All @@ -142,7 +142,7 @@ private static void ImplementInterface(CompositionCode composition, ResolverInfo
code.AppendLine("default:");
using (code.Indent())
{
code.AppendLine($"throw new {Names.SystemNamespace}InvalidOperationException($\"{Names.CannotResolve} \\\"{{tag}}\\\" of type {resolver.Type}.\");");
code.AppendLine($"return base.{Names.ResolveByTagMethodName}(composition, tag);");
}
}

Expand Down

0 comments on commit 8180eba

Please sign in to comment.