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
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
//in startup.cs services.DiscoverInAutoRegisterBL();
The code that is generated is
// <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