RSCG – AutoRegisterInject
RSCG – AutoRegisterInject
name | AutoRegisterInject |
nuget | https://www.nuget.org/packages/AutoRegisterInject/ |
link | https://github.com/patrickklaeren/AutoRegisterInject |
author | Patrick Klaeren |
Generating class DI registration from attributes
This is how you can use AutoRegisterInject .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="AutoRegisterInject" Version="1.2.1" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> </ItemGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> </Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information using AutoRegisterInjectDemo; using Microsoft.Extensions.DependencyInjection; Console.WriteLine("Hello, World!"); ServiceCollection sc = new(); sc.AutoRegisterFromAutoRegisterInjectDemo(); var b=sc.BuildServiceProvider(); var con = b.GetRequiredService<DatabaseCon>(); var db=b.GetRequiredService<IDatabase>(); db.Open();
namespace AutoRegisterInjectDemo; [RegisterScoped] internal class Database : IDatabase { private readonly DatabaseCon con; public Database(DatabaseCon con) { this.con = con; } public void Open() { Console.WriteLine($"open {con.Connection}"); } }
namespace AutoRegisterInjectDemo { internal interface IDatabase { void Open(); } }
namespace AutoRegisterInjectDemo; [RegisterSingleton] internal class DatabaseCon { public string? Connection { get; set; } }
The code that is generated is
// <auto-generated> // Automatically generated by AutoRegisterInject. // Changes made to this file may be lost and may cause undesirable behaviour. // </auto-generated> [System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)] internal sealed class RegisterScopedAttribute : System.Attribute { } [System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)] internal sealed class RegisterSingletonAttribute : System.Attribute { } [System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)] internal sealed class RegisterTransientAttribute : System.Attribute { } [System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)] internal sealed class RegisterHostedServiceAttribute : System.Attribute { }
// <auto-generated> // Automatically generated by AutoRegisterInject. // Changes made to this file may be lost and may cause undesirable behaviour. // </auto-generated> using Microsoft.Extensions.DependencyInjection; public static class AutoRegisterInjectServiceCollectionExtension { public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromAutoRegisterInjectDemo(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection) { return AutoRegister(serviceCollection); } internal static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegister(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection) { serviceCollection.AddScoped<AutoRegisterInjectDemo.IDatabase, AutoRegisterInjectDemo.Database>(); serviceCollection.AddSingleton<AutoRegisterInjectDemo.DatabaseCon>(); return serviceCollection; } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AutoRegisterInject
Leave a Reply