109 lines
5.2 KiB
C#
109 lines
5.2 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;
|
||
|
||
namespace EnvelopeGenerator.DependencyInjection;
|
||
|
||
/// <summary>
|
||
/// Extension methods for registering EnvelopeGenerator services into an <see cref="IServiceCollection"/>.
|
||
/// Use <see cref="AddEnvelopeGenerator"/> 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 – application <em>and</em> infrastructure services – into
|
||
/// the provided <see cref="IServiceCollection"/>.
|
||
/// <para>
|
||
/// Internally this calls <c>AddEnvelopeGeneratorServices</c> (application layer) and
|
||
/// <c>AddEnvelopeGeneratorInfrastructureServices</c> (infrastructure layer).
|
||
/// A <see cref="Microsoft.EntityFrameworkCore.DbContext"/> and / or <c>DbTriggerParams</c> must be
|
||
/// configured through <paramref name="infrastructureOptions"/>; without it no database connection will
|
||
/// be established at runtime.
|
||
/// </para>
|
||
/// </summary>
|
||
/// <param name="services">Service collection to register services into.</param>
|
||
/// <param name="configuration">
|
||
/// Application configuration. Used to bind <c>DispatcherParams</c>, <c>MailParams</c>,
|
||
/// <c>AuthenticatorParams</c>, <c>TotpSmsParams</c>, <c>GtxMessagingParams</c> and other
|
||
/// application-level option sections.
|
||
/// </param>
|
||
/// <param name="infrastructureOptions">
|
||
/// Optional callback to configure the infrastructure layer registration.
|
||
/// Typical usage:
|
||
/// <code>
|
||
/// services.AddEnvelopeGenerator(config, opt =>
|
||
/// {
|
||
/// opt.AddDbContext(o => o.UseSqlServer(connectionString));
|
||
/// opt.AddDbTriggerParams(config);
|
||
/// });
|
||
/// </code>
|
||
/// </param>
|
||
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
||
#pragma warning disable CS0618 // AddEnvelopeGeneratorServices / AddEnvelopeGeneratorInfrastructureServices are intentionally wrapped here
|
||
public static IServiceCollection AddEnvelopeGenerator(
|
||
this IServiceCollection services,
|
||
IConfiguration configuration,
|
||
Action<EnvelopeGenerator.Infrastructure.DependencyInjection.Config>? 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
|
||
|
||
/// <summary>
|
||
/// Registers only the <em>application</em> layer services (MediatR handlers, AutoMapper profiles,
|
||
/// CRUD services, configuration sections) without any infrastructure / database dependencies.
|
||
/// <para>
|
||
/// 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.
|
||
/// </para>
|
||
/// </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.
|
||
/// <para>
|
||
/// Call this in addition to <see cref="AddEnvelopeGenerator"/> 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.
|
||
/// </para>
|
||
/// </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
|
||
}
|