refactor(di): unify Application and Infrastructure DI registrations under a central method

- Added central AddEnvelopeGenerator extension to aggregate existing DI setups
- Introduced EGConfiguration for modular service registration
- Standardized configuration pattern for Application and Infrastructure layers
- Simplified distributed cache and localization registration
This commit is contained in:
tekh 2025-11-03 08:44:22 +01:00
parent 209785dda5
commit 22b494a262
11 changed files with 82 additions and 20 deletions

View File

@ -61,6 +61,9 @@ public static class DependencyInjection
// Add memory cache // Add memory cache
services.AddMemoryCache(); services.AddMemoryCache();
// Register mail services
services.AddScoped<IEnvelopeMailService, EnvelopeMailService>();
return services; return services;
} }
} }

View File

@ -32,7 +32,7 @@ Public Class frmFinalizePDF
#Disable Warning BC40000 ' Type or member is obsolete #Disable Warning BC40000 ' Type or member is obsolete
Factory.Shared _ Factory.Shared _
.BehaveOnPostBuild(PostBuildBehavior.Ignore) _ .BehaveOnPostBuild(PostBuildBehavior.Ignore) _
.AddEnvelopeGeneratorInfrastructureServices( .AddEGInfrastructureServices(
Sub(opt) Sub(opt)
opt.AddDbTriggerParams( opt.AddDbTriggerParams(
Sub(triggers) Sub(triggers)

View File

@ -72,7 +72,7 @@ Namespace Jobs
#Disable Warning BC40000 ' Type or member is obsolete #Disable Warning BC40000 ' Type or member is obsolete
Factory.Shared _ Factory.Shared _
.BehaveOnPostBuild(PostBuildBehavior.Ignore) _ .BehaveOnPostBuild(PostBuildBehavior.Ignore) _
.AddEnvelopeGeneratorInfrastructureServices( .AddEGInfrastructureServices(
Sub(opt) Sub(opt)
opt.AddDbTriggerParams( opt.AddDbTriggerParams(
Sub(triggers) Sub(triggers)

View File

@ -1,11 +1,70 @@
using Microsoft.Extensions.DependencyInjection; using DigitalData.EmailProfilerDispatcher;
using DigitalData.UserManager.DependencyInjection;
using EnvelopeGenerator.Application;
using EnvelopeGenerator.Infrastructure;
using Microsoft.Extensions.Caching.SqlServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using static EnvelopeGenerator.Infrastructure.DependencyInjection;
namespace EnvelopeGenerator.DependencyInjection; namespace EnvelopeGenerator.DependencyInjection;
public static class DependencyInjection public static class DependencyInjection
{ {
public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services) public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration config, Action<EGConfiguration> options, Action<SqlServerCacheOptions> distributedCacheOptions)
{ {
var egConfig = new EGConfiguration();
options.Invoke(egConfig);
egConfig.RegisterAll(services);
// Add envelope generator services
#pragma warning disable CS0618
services.AddUserManager<EGDbContext>();
#pragma warning restore CS0618
services.AddDispatcher<EGDbContext>();
return services; return services;
} }
public record EGConfiguration
{
internal readonly Queue<Action<IServiceCollection>> ServiceRegs = new();
internal void RegisterAll(IServiceCollection services)
{
while (ServiceRegs.Count > 0)
ServiceRegs.Dequeue().Invoke(services);
}
public EGConfiguration AddLocalization()
{
ServiceRegs.Enqueue(s => s.AddLocalization());
return this;
}
public EGConfiguration AddDistributedSqlServerCache(Action<SqlServerCacheOptions> setupAction)
{
ServiceRegs.Enqueue(s => s.AddDistributedSqlServerCache(setupAction));
return this;
}
public EGConfiguration AddInfrastructure(Action<EGInfrastructureConfiguration> options)
{
#pragma warning disable CS0618
ServiceRegs.Enqueue(s => s.AddEGInfrastructureServices(options));
#pragma warning restore CS0618
return this;
}
public IConfiguration Configuration
{
set
{
#pragma warning disable CS0618
ServiceRegs.Enqueue(s => s.AddEnvelopeGeneratorServices(value));
#pragma warning restore CS0618
}
}
}
} }

View File

@ -7,7 +7,14 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.10" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.10" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EnvelopeGenerator.Application\EnvelopeGenerator.Application.csproj" />
<ProjectReference Include="..\EnvelopeGenerator.Infrastructure\EnvelopeGenerator.Infrastructure.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -28,7 +28,7 @@ try
?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration."); ?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration.");
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
builder.Services.AddEnvelopeGeneratorInfrastructureServices( builder.Services.AddEGInfrastructureServices(
opt => opt =>
{ {
opt.AddDbTriggerParams(config); opt.AddDbTriggerParams(config);

View File

@ -185,7 +185,7 @@ try
// Envelope generator serives // Envelope generator serives
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
builder.Services builder.Services
.AddEnvelopeGeneratorInfrastructureServices(opt => .AddEGInfrastructureServices(opt =>
{ {
opt.AddDbTriggerParams(config); opt.AddDbTriggerParams(config);
opt.AddDbContext((provider, options) => opt.AddDbContext((provider, options) =>

View File

@ -38,10 +38,10 @@ namespace EnvelopeGenerator.Infrastructure
/// will be created per HTTP request (or per scope) within the dependency injection container. /// will be created per HTTP request (or per scope) within the dependency injection container.
/// </remarks> /// </remarks>
[Obsolete("Use IRepository")] [Obsolete("Use IRepository")]
public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services, Action<Config> options) public static IServiceCollection AddEGInfrastructureServices(this IServiceCollection services, Action<EGInfrastructureConfiguration> options)
{ {
// configure custom options // configure custom options
options(new Config(services)); options(new EGInfrastructureConfiguration(services));
#if NET #if NET
services.TryAddScoped<IConfigRepository, ConfigRepository>(); services.TryAddScoped<IConfigRepository, ConfigRepository>();
services.TryAddScoped<IDocumentReceiverElementRepository, DocumentReceiverElementRepository>(); services.TryAddScoped<IDocumentReceiverElementRepository, DocumentReceiverElementRepository>();
@ -160,11 +160,11 @@ namespace EnvelopeGenerator.Infrastructure
} }
#endif #endif
public class Config public class EGInfrastructureConfiguration
{ {
private readonly IServiceCollection _services; private readonly IServiceCollection _services;
internal Config(IServiceCollection services) internal EGInfrastructureConfiguration(IServiceCollection services)
{ {
_services = services; _services = services;
} }

View File

@ -525,7 +525,7 @@ namespace EnvelopeGenerator.Infrastructure.Migrations
}); });
}); });
modelBuilder.Entity("EnvelopeGenerator.Domain.Entities.Config", b => modelBuilder.Entity("EnvelopeGenerator.Domain.Entities.EGInfrastructureConfiguration", b =>
{ {
b.Property<string>("ExportPath") b.Property<string>("ExportPath")
.HasColumnType("nvarchar(256)") .HasColumnType("nvarchar(256)")

View File

@ -522,7 +522,7 @@ namespace EnvelopeGenerator.Infrastructure.Migrations
}); });
}); });
modelBuilder.Entity("EnvelopeGenerator.Domain.Entities.Config", b => modelBuilder.Entity("EnvelopeGenerator.Domain.Entities.EGInfrastructureConfiguration", b =>
{ {
b.Property<string>("ExportPath") b.Property<string>("ExportPath")
.HasColumnType("nvarchar(256)") .HasColumnType("nvarchar(256)")

View File

@ -1,4 +1,3 @@
using EnvelopeGenerator.Application.Services;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NLog; using NLog;
using Quartz; using Quartz;
@ -16,7 +15,6 @@ using EnvelopeGenerator.Web.Sanitizers;
using EnvelopeGenerator.Web.Models.Annotation; using EnvelopeGenerator.Web.Models.Annotation;
using DigitalData.UserManager.DependencyInjection; using DigitalData.UserManager.DependencyInjection;
using EnvelopeGenerator.Web.Middleware; using EnvelopeGenerator.Web.Middleware;
using EnvelopeGenerator.Application.Common.Interfaces.Services;
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger(); var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
logger.Info("Logging initialized!"); logger.Info("Logging initialized!");
@ -103,7 +101,7 @@ try
// Add envelope generator services // Add envelope generator services
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
builder.Services.AddEnvelopeGeneratorInfrastructureServices( builder.Services.AddEGInfrastructureServices(
opt => opt =>
{ {
opt.AddDbTriggerParams(config); opt.AddDbTriggerParams(config);
@ -186,11 +184,6 @@ try
builder.Services.Configure<Cultures>(config.GetSection("Cultures")); builder.Services.Configure<Cultures>(config.GetSection("Cultures"));
builder.Services.AddSingleton(sp => sp.GetRequiredService<IOptions<Cultures>>().Value); builder.Services.AddSingleton(sp => sp.GetRequiredService<IOptions<Cultures>>().Value);
// Register mail services
#pragma warning disable CS0618 // Type or member is obsolete
builder.Services.AddScoped<IEnvelopeMailService, EnvelopeMailService>();
#pragma warning restore CS0618 // Type or member is obsolete
builder.Services.AddDispatcher<EGDbContext>(); builder.Services.AddDispatcher<EGDbContext>();
builder.ConfigureBySection<CustomImages>(); builder.ConfigureBySection<CustomImages>();