feat(infrastructure): refactor DIExtensions for DbContext registration

* Removed `dbContextOptions` parameter from `AddEnvelopeGeneratorInfrastructureServices`
* Moved `AddDbContext` registration into `Config` class with overloads for both `Action<DbContextOptionsBuilder>` and `Action<IServiceProvider, DbContextOptionsBuilder>`
* Simplified service registration flow and improved extensibility
This commit is contained in:
tekh 2025-10-01 13:25:52 +02:00
parent 9472322c1d
commit 82d4b7ec6a
3 changed files with 34 additions and 18 deletions

View File

@ -185,7 +185,19 @@ try
// Envelope generator serives
#pragma warning disable CS0618 // Type or member is obsolete
builder.Services
.AddEnvelopeGeneratorInfrastructureServices(sqlExecutorConfigureOptions: executor => executor.ConnectionString = connStr)
.AddEnvelopeGeneratorInfrastructureServices(opt =>
{
opt.ConfigureDbTriggerParams(config);
opt.AddDbContext((provider, options) =>
{
var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
options.UseSqlServer(connStr)
.LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
});
},
sqlExecutorConfigureOptions: executor => executor.ConnectionString = connStr)
.AddEnvelopeGeneratorServices(config);
#pragma warning restore CS0618 // Type or member is obsolete

View File

@ -39,12 +39,7 @@ namespace EnvelopeGenerator.Infrastructure
/// </remarks>
[Obsolete("Use IRepository")]
public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services,
Action<Config> options,
Action<IServiceProvider, DbContextOptionsBuilder>
#if NET
?
#endif
dbContextOptions = null
Action<Config> options
#if NET
, IConfiguration? sqlExecutorConfiguration = null,
Action<SQLExecutorParams>? sqlExecutorConfigureOptions = null
@ -53,10 +48,6 @@ namespace EnvelopeGenerator.Infrastructure
{
// configure custom options
options(new Config(services));
if (dbContextOptions != null)
services.AddDbContext<EGDbContext>(dbContextOptions);
#if NET
services.TryAddScoped<IConfigRepository, ConfigRepository>();
services.TryAddScoped<IDocumentReceiverElementRepository, DocumentReceiverElementRepository>();
@ -197,6 +188,16 @@ namespace EnvelopeGenerator.Infrastructure
{
_services.Configure(configureOptions);
}
public void AddDbContext(Action<DbContextOptionsBuilder> dbContextOptions)
{
_services.AddDbContext<EGDbContext>(dbContextOptions);
}
public void AddDbContext(Action<IServiceProvider, DbContextOptionsBuilder> dbContextOptions)
{
_services.AddDbContext<EGDbContext>(dbContextOptions);
}
}
}
}

View File

@ -104,8 +104,10 @@ try
// Add envelope generator services
#pragma warning disable CS0618 // Type or member is obsolete
builder.Services.AddEnvelopeGeneratorInfrastructureServices(
opt => opt.ConfigureDbTriggerParams(config),
(provider, options) =>
opt =>
{
opt.ConfigureDbTriggerParams(config);
opt.AddDbContext((provider, options) =>
{
var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
options.UseSqlServer(connStr)
@ -113,6 +115,7 @@ try
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
});
});
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete