From 83957d28e9f8e7e291a6df863107a1cf9f5c75e9 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 28 May 2026 16:27:30 +0200 Subject: [PATCH] Add DependencyInjection class for service registration in EnvelopeGenerator --- .../DependencyInjection.cs | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 EnvelopeGenerator.DependencyInjection/DependencyInjection.cs diff --git a/EnvelopeGenerator.DependencyInjection/DependencyInjection.cs b/EnvelopeGenerator.DependencyInjection/DependencyInjection.cs new file mode 100644 index 00000000..cebe6df9 --- /dev/null +++ b/EnvelopeGenerator.DependencyInjection/DependencyInjection.cs @@ -0,0 +1,108 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using EnvelopeGenerator.Application; +using EnvelopeGenerator.Application.Common.Interfaces.Services; +using EnvelopeGenerator.Application.Services; +using EnvelopeGenerator.Infrastructure; + +namespace EnvelopeGenerator.DependencyInjection; + +/// +/// Extension methods for registering EnvelopeGenerator services into an . +/// Use as the single entry-point for projects that need both the +/// application layer (MediatR, AutoMapper, CRUD services, configuration sections) and the infrastructure +/// layer (repositories, DbContext, SQL executors). +/// For projects that do not need a database (e.g. lightweight API gateways or unit-test hosts), use +/// to register only the application layer. +/// +public static class DependencyInjection +{ + /// + /// Registers the full EnvelopeGenerator stack – application and infrastructure services – into + /// the provided . + /// + /// Internally this calls AddEnvelopeGeneratorServices (application layer) and + /// AddEnvelopeGeneratorInfrastructureServices (infrastructure layer). + /// A and / or DbTriggerParams must be + /// configured through ; without it no database connection will + /// be established at runtime. + /// + /// + /// Service collection to register services into. + /// + /// Application configuration. Used to bind DispatcherParams, MailParams, + /// AuthenticatorParams, TotpSmsParams, GtxMessagingParams and other + /// application-level option sections. + /// + /// + /// Optional callback to configure the infrastructure layer registration. + /// Typical usage: + /// + /// services.AddEnvelopeGenerator(config, opt => + /// { + /// opt.AddDbContext(o => o.UseSqlServer(connectionString)); + /// opt.AddDbTriggerParams(config); + /// }); + /// + /// + /// The updated . +#pragma warning disable CS0618 // AddEnvelopeGeneratorServices / AddEnvelopeGeneratorInfrastructureServices are intentionally wrapped here + public static IServiceCollection AddEnvelopeGenerator( + this IServiceCollection services, + IConfiguration configuration, + Action? infrastructureOptions = null) + { + // Application layer: CRUD services, MediatR, AutoMapper, configuration sections. + services.AddEnvelopeGeneratorServices(configuration); + + // Infrastructure layer: repositories, DbContext, Dapper type maps, SQL executors. + services.AddEnvelopeGeneratorInfrastructureServices(opt => + { + infrastructureOptions?.Invoke(opt); + }); + + return services; + } +#pragma warning restore CS0618 + + /// + /// Registers only the application layer services (MediatR handlers, AutoMapper profiles, + /// CRUD services, configuration sections) without any infrastructure / database dependencies. + /// + /// Useful for projects that already manage their own DbContext or do not require direct database + /// access, such as lightweight API gateways, console tools or unit/integration test hosts that + /// use an in-memory database configured elsewhere. + /// + /// + /// Service collection to register services into. + /// Application configuration used to bind application-level option sections. + /// The updated . +#pragma warning disable CS0618 + public static IServiceCollection AddEnvelopeGeneratorCore( + this IServiceCollection services, + IConfiguration configuration) + { + services.AddEnvelopeGeneratorServices(configuration); + return services; + } +#pragma warning restore CS0618 + + /// + /// Registers as the scoped + /// implementation. + /// + /// Call this in addition to when the consuming project needs to + /// send envelope e-mails directly (e.g. a Worker Service or the Web project). Projects that rely + /// purely on MediatR commands to trigger mail delivery do not need to call this. + /// + /// + /// Service collection to register services into. + /// The updated . +#pragma warning disable CS0618 + public static IServiceCollection AddEnvelopeMailService(this IServiceCollection services) + { + services.AddScoped(); + return services; + } +#pragma warning restore CS0618 +}