From 6f0fb3effbe38bcb3366996568a93a018dd53a82 Mon Sep 17 00:00:00 2001 From: "Jeremy D. Miller" Date: Mon, 19 Feb 2024 14:23:40 -0600 Subject: [PATCH] New ICodeFileCollectionWithServices marker interface that Wolverine will need. Making code file collections opt in to using the IoC code generation. Bumps to 3.5.0 --- Directory.Build.props | 2 +- src/GeneratorTarget/GreeterGenerator.cs | 4 ++++ src/JasperFx.CodeGeneration/DynamicCodeBuilder.cs | 6 ++++-- src/JasperFx.CodeGeneration/ICodeFileCollection.cs | 6 ++++++ src/JasperFx.CodeGeneration/JasperFx.CodeGeneration.csproj | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 2facad9..fd58f0e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 3.4.1 + 3.5.0 10.0 Jeremy D. Miller;Babu Annamalai;Oskar Dudycz;Joona-Pekka Kokko net6.0;net7.0;net8.0 diff --git a/src/GeneratorTarget/GreeterGenerator.cs b/src/GeneratorTarget/GreeterGenerator.cs index ff0a11e..1112e8f 100644 --- a/src/GeneratorTarget/GreeterGenerator.cs +++ b/src/GeneratorTarget/GreeterGenerator.cs @@ -70,6 +70,8 @@ public IReadOnlyList BuildFiles() }; } + public bool ShouldUseContainerServices { get; } = false; + public string ChildNamespace { get; } = "Helpers.Greeters"; public GenerationRules Rules { get; } = new GenerationRules @@ -91,6 +93,8 @@ public IReadOnlyList BuildFiles() }; } + public bool ShouldUseContainerServices { get; } = false; + public string ChildNamespace { get; } = "Helpers.Greeters2"; public GenerationRules Rules { get; } = new GenerationRules diff --git a/src/JasperFx.CodeGeneration/DynamicCodeBuilder.cs b/src/JasperFx.CodeGeneration/DynamicCodeBuilder.cs index 4b7d623..9543f93 100644 --- a/src/JasperFx.CodeGeneration/DynamicCodeBuilder.cs +++ b/src/JasperFx.CodeGeneration/DynamicCodeBuilder.cs @@ -66,7 +66,6 @@ public string GenerateCodeFor(string childNamespace) public void WriteGeneratedCode(Action onFileWritten) { - foreach (var collection in Collections) { var directory = collection.Rules.GeneratedCodeOutputPath.ToFullPath(); @@ -102,7 +101,10 @@ private string generateCode(ICodeFileCollection collection) var files = collection.BuildFiles(); foreach (var file in files) file.AssembleTypes(generatedAssembly); - return generatedAssembly.GenerateCode(ServiceVariableSource); + // This was important. Each source code collection should explicitly opt into using IoC services rather + // than making that automatic + var services = collection is ICodeFileCollectionWithServices ? ServiceVariableSource : null; + return generatedAssembly.GenerateCode(services); } /// diff --git a/src/JasperFx.CodeGeneration/ICodeFileCollection.cs b/src/JasperFx.CodeGeneration/ICodeFileCollection.cs index aba15c8..8d3bf2c 100644 --- a/src/JasperFx.CodeGeneration/ICodeFileCollection.cs +++ b/src/JasperFx.CodeGeneration/ICodeFileCollection.cs @@ -2,6 +2,12 @@ namespace JasperFx.CodeGeneration; +/// +/// Code file collection that should be generated with IoC sourced +/// dependencies. This was originally built to support Wolverine message handlers +/// +public interface ICodeFileCollectionWithServices : ICodeFileCollection{} + public interface ICodeFileCollection { /// diff --git a/src/JasperFx.CodeGeneration/JasperFx.CodeGeneration.csproj b/src/JasperFx.CodeGeneration/JasperFx.CodeGeneration.csproj index 4fadb49..08e35d0 100644 --- a/src/JasperFx.CodeGeneration/JasperFx.CodeGeneration.csproj +++ b/src/JasperFx.CodeGeneration/JasperFx.CodeGeneration.csproj @@ -6,7 +6,7 @@ - +