RSCG – ServiceScan.SourceGenerator
RSCG – ServiceScan.SourceGenerator
name | ServiceScan.SourceGenerator |
nuget | https://www.nuget.org/packages/ServiceScan.SourceGenerator/ |
link | https://github.com/Dreamescaper/ServiceScan.SourceGenerator |
author | Oleksandr Liakhevych |
Generating service collection / DI registration
This is how you can use ServiceScan.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="ServiceScan.SourceGenerator" Version="1.1.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> </ItemGroup> </Project>
The code that you will use is
using InjectDemo; using Microsoft.Extensions.DependencyInjection; var sc=new ServiceCollection(); sc.AddMyServices(); var sp=sc.BuildServiceProvider(); var con = sp.GetService(typeof(Database)) as IDatabase; ArgumentNullException.ThrowIfNull(con); con.Open(); public static partial class MyServiceProvider { [ServiceScan.SourceGenerator.GenerateServiceRegistrations(AssignableTo = typeof(Database),AsSelf =true, Lifetime = ServiceLifetime.Scoped)] [ServiceScan.SourceGenerator.GenerateServiceRegistrations(AssignableTo = typeof(IDatabase), Lifetime = ServiceLifetime.Scoped)] public static partial IServiceCollection AddMyServices(this IServiceCollection services) ; }
namespace InjectDemo; 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(); } }
namespace InjectDemo; 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
#nullable enable using System; using System.Diagnostics; using Microsoft.Extensions.DependencyInjection; namespace ServiceScan.SourceGenerator; [Conditional("CODE_ANALYSIS")] [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] internal class GenerateServiceRegistrationsAttribute : Attribute { /// <summary> /// Set the assembly containing the given type as the source of types to register. /// If not specified, the assembly containing the method with this attribute will be used. /// </summary> public Type? FromAssemblyOf { get; set; } /// <summary> /// Set the type that the registered types must be assignable to. /// Types will be registered with this type as the service type, /// unless <see cref="AsImplementedInterfaces"/> or <see cref="AsSelf"/> is set. /// </summary> public Type? AssignableTo { get; set; } /// <summary> /// Set the lifetime of the registered services. /// <see cref="ServiceLifetime.Transient"/> is used if not specified. /// </summary> public ServiceLifetime Lifetime { get; set; } /// <summary> /// If set to true, types will be registered as implemented interfaces instead of their actual type. /// </summary> public bool AsImplementedInterfaces { get; set; } /// <summary> /// If set to true, types will be registered with their actual type. /// It can be combined with <see cref="AsImplementedInterfaces"/>, in that case implemeted interfaces will be /// "forwarded" to "self" implementation. /// </summary> public bool AsSelf { get; set; } /// <summary> /// Set this value to filter the types to register by their full name. /// You can use '*' wildcards. /// You can also use ',' to separate multiple filters. /// </summary> /// <example>Namespace.With.Services.*</example> /// <example>*Service,*Factory</example> public string? TypeNameFilter { get; set; } }
using Microsoft.Extensions.DependencyInjection; public static partial class MyServiceProvider { public static partial IServiceCollection AddMyServices(this IServiceCollection services) { return services .AddScoped<InjectDemo.Database, InjectDemo.Database>() .AddScoped<InjectDemo.IDatabase, InjectDemo.Database>() .AddScoped<InjectDemo.IDatabase, InjectDemo.DatabaseCon>(); } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/ServiceScan.SourceGenerator
Leave a Reply