RSCG Example–SourceInject- part 22
name | SourceInject |
nuget |
https://www.nuget.org/packages/SourceInject/ |
link | https://github.com/giggio/sourceinject |
author | Giovanni Bassi |
Auto register services in startup 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | using Microsoft.Extensions.DependencyInjection; using System; using System.Threading.Tasks; namespace AutoRegisterBL { [Inject(ServiceLifetime.Transient)] public class Repo { public async Task<person> GetFromId( int id) { await Task.Delay(1000); return new Person() { ID = id, Name = " Andrei Ignat " + id }; } } } |
The code that you will use is
1 2 3 | //in startup.cs services.DiscoverInAutoRegisterBL(); |
The code that is generated is
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 | // <auto-generated> using Microsoft.Extensions.DependencyInjection; public static class GeneratedServicesExtension { public static void DiscoverInAutoRegisterBL( this IServiceCollection services) => services.Discover(); internal static void Discover( this IServiceCollection services) { services.AddTransient<autoregisterbl.repo>(); } } public static class AutoRegisterBLDiscoverer { public static void Discover(IServiceCollection services) => services.Discover(); } |
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/AutoRegister
Leave a Reply