Introduced `EnvelopeGeneratorOptions` and `SqlCacheOptions` to enable fine-grained control over optional service registrations in `AddEnvelopeGenerator`. Updated `AddEnvelopeGenerator` to conditionally register services like `HttpContextAccessor`, `DistributedSqlServerCache`, `Dispatcher`, `MemoryCache`, and `UserManager` based on these options. Updated `DependencyInjection.csproj` to include necessary framework references and package dependencies. Simplified `Program.cs` by consolidating service registrations into `AddEnvelopeGenerator`, reducing boilerplate and improving maintainability. Improved extensibility by centralizing service registration logic, allowing consuming projects to customize configurations without modifying the core library. Updated documentation and removed unused directives.
146 lines
6.0 KiB
C#
146 lines
6.0 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// Controls which optional services are registered by <see cref="DependencyInjection.AddEnvelopeGenerator"/>.
|
|
/// All flags default to <c>true</c>. Set a flag to <c>false</c> if the consuming project
|
|
/// already registers that service itself or simply does not need it.
|
|
/// </summary>
|
|
public sealed class EnvelopeGeneratorOptions
|
|
{
|
|
/// <summary>Calls <c>AddHttpContextAccessor()</c>. Default: <c>true</c>.</summary>
|
|
public bool AddHttpContextAccessor { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// Calls <c>AddDistributedSqlServerCache()</c> with the supplied <see cref="SqlCacheOptions"/>.
|
|
/// Requires <see cref="SqlCacheOptions"/> to be configured. Default: <c>true</c>.
|
|
/// </summary>
|
|
public bool AddDistributedSqlServerCache { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// Options for the distributed SQL Server cache.
|
|
/// Required when <see cref="AddDistributedSqlServerCache"/> is <c>true</c>.
|
|
/// </summary>
|
|
public SqlCacheOptions? SqlCacheOptions { get; set; }
|
|
|
|
/// <summary>Calls <c>AddDispatcher<TDbContext>()</c>. Default: <c>true</c>.</summary>
|
|
public bool AddDispatcher { get; set; } = true;
|
|
|
|
/// <summary>Calls <c>AddMemoryCache()</c>. Default: <c>true</c>.</summary>
|
|
public bool AddMemoryCache { get; set; } = true;
|
|
|
|
/// <summary>Calls <c>AddUserManager<TDbContext>()</c>. Default: <c>true</c>.</summary>
|
|
public bool AddUserManager { get; set; } = true;
|
|
}
|
|
|
|
/// <summary>Options for <c>AddDistributedSqlServerCache</c>.</summary>
|
|
public sealed class SqlCacheOptions
|
|
{
|
|
/// <summary>SQL Server connection string.</summary>
|
|
public string ConnectionString { get; set; } = string.Empty;
|
|
|
|
/// <summary>Schema name. Default: <c>dbo</c>.</summary>
|
|
public string SchemaName { get; set; } = "dbo";
|
|
|
|
/// <summary>Table name. Default: <c>TBDD_CACHE</c>.</summary>
|
|
public string TableName { get; set; } = "TBDD_CACHE";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extension methods for registering EnvelopeGenerator services into an <see cref="IServiceCollection"/>.
|
|
/// Use <see cref="AddEnvelopeGenerator{TDbContext}"/> 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
|
|
/// <see cref="AddEnvelopeGeneratorCore"/> to register only the application layer.
|
|
/// </summary>
|
|
public static class DependencyInjection
|
|
{
|
|
/// <summary>
|
|
/// Registers the full EnvelopeGenerator stack using <see cref="EGDbContext"/> as the DbContext type.
|
|
/// </summary>
|
|
public static IServiceCollection AddEnvelopeGenerator(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
Action<EnvelopeGenerator.Infrastructure.DependencyInjection.Config>? infrastructureOptions = null,
|
|
Action<EnvelopeGeneratorOptions>? 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<EGDbContext>();
|
|
|
|
if (opt.AddMemoryCache)
|
|
services.AddMemoryCache();
|
|
|
|
#pragma warning disable CS0618
|
|
if (opt.AddUserManager)
|
|
services.AddUserManager<EGDbContext>();
|
|
#pragma warning restore CS0618
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers only the <em>application</em> layer services (MediatR handlers, AutoMapper profiles,
|
|
/// CRUD services, configuration sections) without any infrastructure / database dependencies.
|
|
/// </summary>
|
|
/// <param name="services">Service collection to register services into.</param>
|
|
/// <param name="configuration">Application configuration used to bind application-level option sections.</param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
|
#pragma warning disable CS0618
|
|
public static IServiceCollection AddEnvelopeGeneratorCore(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
services.AddEnvelopeGeneratorServices(configuration);
|
|
return services;
|
|
}
|
|
#pragma warning restore CS0618
|
|
|
|
/// <summary>
|
|
/// Registers <see cref="EnvelopeMailService"/> as the <see cref="IEnvelopeMailService"/> scoped
|
|
/// implementation.
|
|
/// </summary>
|
|
/// <param name="services">Service collection to register services into.</param>
|
|
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
|
#pragma warning disable CS0618
|
|
public static IServiceCollection AddEnvelopeMailService(this IServiceCollection services)
|
|
{
|
|
services.AddScoped<IEnvelopeMailService, EnvelopeMailService>();
|
|
return services;
|
|
}
|
|
#pragma warning restore CS0618
|
|
}
|