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
<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
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();
using DependencyModules.Runtime.Attributes; [DependencyModule] public partial class MyModule { }
namespace InjectDemo { internal interface IDatabase { public void Open(); } }
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(); } }
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
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)); } }
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