- builder now implements iFactoryBuilder that is called from registration

pull/62/head
Simon G. 4 days ago
parent daad788b2e
commit 0ac89506c1
Signed by: SimonG
GPG Key ID: 0B82B964BA536523
  1. 26
      LightweightIocContainer.FactoryGenerator/FactoryGenerator.cs

@ -14,7 +14,7 @@ namespace LightweightIocContainer.FactoryGenerator;
public class FactoryGenerator : IIncrementalGenerator
{
private const string EXTENSION_CLASS_NAME = "FactoryExtensions";
private const string BUILDER_CLASS_NAME = "GeneratedFactoryBuilder";
private const string BUILDER_CLASS_NAME = "FactoryBuilder";
private const string GENERATED_FILE_HEADER = "//---GENERATED by FactoryGenerator! DO NOT EDIT!---";
private const string INDENT = " ";
@ -25,6 +25,7 @@ public class FactoryGenerator : IIncrementalGenerator
context.RegisterPostInitializationOutput(c => c.AddSource($"{EXTENSION_CLASS_NAME}.g.cs", GenerateFactoryExtensionsClass(classNamespace, EXTENSION_CLASS_NAME)));
IncrementalValuesProvider<ITypeSymbol?> syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(IsCallToGenerateFactory, GetTypeArgument);
context.RegisterSourceOutput(syntaxProvider.Collect(), GenerateTypeDependentClasses);
}
@ -35,8 +36,8 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine(GENERATED_FILE_HEADER);
stringBuilder.AppendLine();
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Factories;");
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Registrations;");
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Registrations.Fluent;");
stringBuilder.AppendLine();
if (!string.IsNullOrEmpty(classNamespace))
@ -50,8 +51,8 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine($"{INDENT}public static IRegistrationBase WithGeneratedFactory<TFactory>(this IRegistrationBase registration)");
stringBuilder.AppendLine($"{INDENT}{{");
stringBuilder.AppendLine($"{INDENT}{INDENT}TFactory factory = Builder.Create<TFactory>();");
stringBuilder.AppendLine($"{INDENT}{INDENT}registration.AddGeneratedFactory<TFactory>(factory);");
stringBuilder.AppendLine($"{INDENT}{INDENT}FactoryBuilder factoryBuilder = new();");
stringBuilder.AppendLine($"{INDENT}{INDENT}registration.AddGeneratedFactory<TFactory>(factoryBuilder);");
stringBuilder.AppendLine();
stringBuilder.AppendLine($"{INDENT}{INDENT}return registration;");
stringBuilder.AppendLine($"{INDENT}}}");
@ -95,6 +96,14 @@ public class FactoryGenerator : IIncrementalGenerator
context.AddSource($"{BUILDER_CLASS_NAME}.g.cs", GenerateBuilderClassSourceCode(classNamespace, types));
}
private void GenerateFactory(SourceProductionContext context, ITypeSymbol? typeSymbol)
{
if (typeSymbol is null)
return;
context.AddSource($"Generated{typeSymbol.Name}.g.cs", GenerateFactorySourceCode(typeSymbol));
}
private string GenerateBuilderClassSourceCode(string? classNamespace, ImmutableArray<ITypeSymbol?> types)
{
StringBuilder stringBuilder = new();
@ -102,6 +111,9 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine(GENERATED_FILE_HEADER);
stringBuilder.AppendLine();
stringBuilder.AppendLine("using LightweightIocContainer.Interfaces.Factories;");
stringBuilder.AppendLine("using LightweightIocContainer.Factories;");
foreach (string typeNamespace in GetNamespacesOfTypes(types))
stringBuilder.AppendLine($"using {typeNamespace};");
@ -113,9 +125,9 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine();
}
stringBuilder.AppendLine("public static class Builder");
stringBuilder.AppendLine("public class FactoryBuilder : IFactoryBuilder");
stringBuilder.AppendLine("{");
stringBuilder.AppendLine($"{INDENT}public static TFactory Create<TFactory>()");
stringBuilder.AppendLine($"{INDENT}public TFactory Create<TFactory>(IocContainer container)");
stringBuilder.AppendLine($"{INDENT}{{");
foreach (ITypeSymbol? type in types)
@ -125,7 +137,7 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine($"{INDENT}{INDENT}if (typeof(TFactory) == typeof({type.Name}))");
stringBuilder.AppendLine($"{INDENT}{INDENT}{{");
stringBuilder.AppendLine($"{INDENT}{INDENT}{INDENT}return (TFactory) (object) new Generated{type.Name}();");
stringBuilder.AppendLine($"{INDENT}{INDENT}{INDENT}return (TFactory) (object) new Generated{type.Name}(container, new FactoryHelper());");
stringBuilder.AppendLine($"{INDENT}{INDENT}}}");
stringBuilder.AppendLine();
}

Loading…
Cancel
Save