RSCG – DependencyModules.SourceGenerator
RSCG – DependencyModules.SourceGenerator
name | DependencyModules.SourceGenerator |
nuget |
https://www.nuget.org/packages/DependencyModules.SourceGenerator/ https://www.nuget.org/packages/DependencyModules.Runtime/ |
link | https://github.com/ipjohnson/DependencyModules |
author | Ian Johnson |
Generating service dependencies from attributes.
Also,by the author, a more advanced example you will find in the DemoWithTest.zip inside the zip file
This is how you can use DependencyModules.SourceGenerator .
The code that you start with is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | < Project Sdk = "Microsoft.NET.Sdk" > < PropertyGroup > < OutputType >Exe</ OutputType > < TargetFramework >net8.0</ TargetFramework > < ImplicitUsings >enable</ ImplicitUsings > < Nullable >enable</ Nullable > </ PropertyGroup > < PropertyGroup > < EmitCompilerGeneratedFiles >true</ EmitCompilerGeneratedFiles > < CompilerGeneratedFilesOutputPath >$(BaseIntermediateOutputPath)\GX</ CompilerGeneratedFilesOutputPath > </ PropertyGroup > < ItemGroup > < PackageReference Include = "DependencyModules.Runtime" Version = "1.0.0-RC9074" /> < PackageReference Include = "DependencyModules.SourceGenerator" Version = "1.0.0-RC9074" /> < PackageReference Include = "Microsoft.Extensions.DependencyInjection" Version = "9.0.2" /> </ ItemGroup > </ Project > |
The code that you will use is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | using DependencyModules.Runtime; using InjectDemo; using Microsoft.Extensions.DependencyInjection; var serviceCollection = new ServiceCollection(); serviceCollection.AddModule<MyModule>(); var provider = serviceCollection.BuildServiceProvider(); var service = provider.GetService<Database>(); if (service == null ) throw new Exception( "Service not found" ); else service.Open(); |
1 2 3 4 5 6 7 | using DependencyModules.Runtime.Attributes; [DependencyModule] public partial class MyModule { } |
1 2 3 4 5 6 7 | namespace InjectDemo { internal interface IDatabase { public void Open(); } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | using DependencyModules.Runtime.Attributes; namespace InjectDemo; [SingletonService(ServiceType = typeof (Database))] partial class Database : IDatabase { private readonly IDatabase con; public Database(IDatabase con) { this .con = con; } public void Open() { Console.WriteLine($ "open from database" ); con.Open(); } } |
01 02 03 04 05 06 07 08 09 10 11 12 | using DependencyModules.Runtime.Attributes; namespace InjectDemo; [SingletonService] public partial class DatabaseCon:IDatabase { public string ? Connection { get ; set ; } public void Open() { Console.WriteLine( "open from database con" ); } } |
The code that is generated is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | using DependencyModules.Runtime.Helpers; using InjectDemo; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; public partial class MyModule { private static int moduleField = DependencyRegistry<MyModule>.Add(ModuleDependencies); private static void ModuleDependencies(IServiceCollection services) { services.AddSingleton( typeof (Database), typeof (Database)); services.AddSingleton( typeof (IDatabase), typeof (DatabaseCon)); } } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using BaseAttribute = System.Attribute; using DependencyModules.Runtime.Helpers; using DependencyModules.Runtime.Interfaces; using Microsoft.Extensions.DependencyInjection; #nullable enable public partial class MyModule : IDependencyModule { static MyModule() { } public void PopulateServiceCollection(IServiceCollection services) { DependencyRegistry<MyModule>.LoadModules(services, this ); } void IDependencyModule.InternalApplyServices(IServiceCollection services) { DependencyRegistry<MyModule>.ApplyServices(services); } public override bool Equals( object ? obj) { return obj is MyModule; } public override int GetHashCode() { return HashCode.Combine( base .GetHashCode()); } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly | AttributeTargets.Method | AttributeTargets.Parameter, AllowMultiple = true )] public partial class Attribute : BaseAttribute, IDependencyModuleProvider { public IDependencyModule GetModule() { var newModule = new MyModule(); return newModule; } } } #nullable disable |
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/DependencyModules.SourceGenerator
Leave a Reply