using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using EnvelopeGenerator.Application; using EnvelopeGenerator.Application.Common.Interfaces.Services; using EnvelopeGenerator.Application.Services; using EnvelopeGenerator.Infrastructure; using DigitalData.EmailProfilerDispatcher; using DigitalData.UserManager.DependencyInjection; namespace EnvelopeGenerator.DependencyInjection; /// /// Controls which optional services are registered by . /// All flags default to true. Set a flag to false if the consuming project /// already registers that service itself or simply does not need it. /// public sealed class EnvelopeGeneratorOptions { /// Calls AddHttpContextAccessor(). Default: true. public bool AddHttpContextAccessor { get; set; } = true; /// /// Calls AddDistributedSqlServerCache() with the supplied . /// Requires to be configured. Default: true. /// public bool AddDistributedSqlServerCache { get; set; } = true; /// /// Options for the distributed SQL Server cache. /// Required when is true. /// public SqlCacheOptions? SqlCacheOptions { get; set; } /// Calls AddDispatcher<TDbContext>(). Default: true. public bool AddDispatcher { get; set; } = true; /// Calls AddMemoryCache(). Default: true. public bool AddMemoryCache { get; set; } = true; /// Calls AddUserManager<TDbContext>(). Default: true. public bool AddUserManager { get; set; } = true; } /// Options for AddDistributedSqlServerCache. public sealed class SqlCacheOptions { /// SQL Server connection string. public string ConnectionString { get; set; } = string.Empty; /// Schema name. Default: dbo. public string SchemaName { get; set; } = "dbo"; /// Table name. Default: TBDD_CACHE. public string TableName { get; set; } = "TBDD_CACHE"; } /// /// 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 using as the DbContext type. /// public static IServiceCollection AddEnvelopeGenerator( this IServiceCollection services, IConfiguration configuration, Action? infrastructureOptions = null, Action? options = null) { var opt = new EnvelopeGeneratorOptions(); options?.Invoke(opt); #pragma warning disable CS0618 // Application layer: CRUD services, MediatR, AutoMapper, configuration sections. services.AddEnvelopeGeneratorServices(configuration); // Infrastructure layer: repositories, DbContext, Dapper type maps, SQL executors. services.AddEnvelopeGeneratorInfrastructureServices(cfg => { infrastructureOptions?.Invoke(cfg); }); #pragma warning restore CS0618 if (opt.AddHttpContextAccessor) services.AddHttpContextAccessor(); if (opt.AddDistributedSqlServerCache && opt.SqlCacheOptions is { } cacheOpts) services.AddDistributedSqlServerCache(o => { o.ConnectionString = cacheOpts.ConnectionString; o.SchemaName = cacheOpts.SchemaName; o.TableName = cacheOpts.TableName; }); if (opt.AddDispatcher) services.AddDispatcher(); if (opt.AddMemoryCache) services.AddMemoryCache(); #pragma warning disable CS0618 if (opt.AddUserManager) services.AddUserManager(); #pragma warning restore CS0618 return services; } /// /// Registers only the application layer services (MediatR handlers, AutoMapper profiles, /// CRUD services, configuration sections) without any infrastructure / database dependencies. /// /// 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. /// /// 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 }