Refactor envelope generator service dependencies

This commit replaces the `AddEnvelopeGeneratorRepositories` method with `AddEnvelopeGeneratorInfrastructureServices`, allowing for more flexible configuration through `IConfiguration` and `Action<SQLExecutorParams>`. The `SQLExecutor` class now utilizes `SQLExecutorParams` for its connection string, enhancing configurability. A new `SQLExecutorParams` class has been introduced to encapsulate connection string management. Various service registration calls have been updated to integrate the new infrastructure services, improving modularity and ease of database connection management.
This commit is contained in:
Developer 02 2025-05-05 10:47:00 +02:00
parent a757749767
commit a42e4287ff
8 changed files with 47 additions and 11 deletions

View File

@ -159,7 +159,7 @@ builder.Services.AddCookieBasedLocalizer() ;
// Envelope generator serives // Envelope generator serives
builder.Services builder.Services
.AddEnvelopeGeneratorRepositories() .AddEnvelopeGeneratorInfrastructureServices(sqlExecutorConfigureOptions: executor => executor.ConnectionString = connStr)
.AddEnvelopeGeneratorServices(config); .AddEnvelopeGeneratorServices(config);
var app = builder.Build(); var app = builder.Build();

View File

@ -7,6 +7,7 @@ using DigitalData.Core.Infrastructure;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
using DigitalData.Core.Infrastructure.AutoMapper; using DigitalData.Core.Infrastructure.AutoMapper;
using EnvelopeGenerator.Application.Contracts.SQLExecutor; using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using Microsoft.Extensions.Configuration;
namespace EnvelopeGenerator.Infrastructure; namespace EnvelopeGenerator.Infrastructure;
@ -26,7 +27,10 @@ public static class DIExtensions
/// This method ensures that the repositories are registered as scoped services, meaning that a new instance of each repository /// This method ensures that the repositories are registered as scoped services, meaning that a new instance of each repository
/// 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>
public static IServiceCollection AddEnvelopeGeneratorRepositories(this IServiceCollection services, Action<DbContextOptionsBuilder>? dbContextOptions = null) public static IServiceCollection AddEnvelopeGeneratorInfrastructureServices(this IServiceCollection services,
Action<DbContextOptionsBuilder>? dbContextOptions = null,
IConfiguration? sqlExecutorConfiguration = null,
Action<SQLExecutorParams>? sqlExecutorConfigureOptions = null)
{ {
if(dbContextOptions is not null) if(dbContextOptions is not null)
services.AddDbContext<EGDbContext>(dbContextOptions); services.AddDbContext<EGDbContext>(dbContextOptions);
@ -66,11 +70,34 @@ public static class DIExtensions
services.AddSQLExecutor<DocumentReceiverElement>(); services.AddSQLExecutor<DocumentReceiverElement>();
services.AddSQLExecutor<DocumentStatus>(); services.AddSQLExecutor<DocumentStatus>();
if (sqlExecutorConfiguration is not null || sqlExecutorConfigureOptions is not null)
services.AddSQLExecutor(sqlExecutorConfiguration, sqlExecutorConfigureOptions);
return services; return services;
} }
public static IServiceCollection AddSQLExecutor<T>(this IServiceCollection services) where T : class public static IServiceCollection AddSQLExecutor(this IServiceCollection services, IConfiguration? configuration = null, Action<SQLExecutorParams>? configureOptions = null)
{ {
if(configuration is not null && configureOptions is not null)
throw new InvalidOperationException("Cannot use both 'configuration' and 'configureOptions'. Only one should be provided.");
if (configuration is not null)
services.Configure<SQLExecutorParams>(configuration);
if(configureOptions is not null)
services.Configure(configureOptions);
return services.AddSingleton<ISQLExecutor, SQLExecutor>();
}
public static IServiceCollection AddSQLExecutor<T>(this IServiceCollection services, IConfiguration? configuration = null, Action<SQLExecutorParams>? configureOptions = null) where T : class
{
if (configuration is not null && configureOptions is not null)
throw new InvalidOperationException("Cannot use both 'configuration' and 'configureOptions'. Only one should be provided.");
if (configuration is not null)
services.Configure<SQLExecutorParams>(configuration);
services.AddScoped<ISQLExecutor<T>, SQLExecutor<T>>(); services.AddScoped<ISQLExecutor<T>, SQLExecutor<T>>();
var interfaceType = typeof(ISQL<>); var interfaceType = typeof(ISQL<>);

View File

@ -2,23 +2,25 @@
using EnvelopeGenerator.Application.Contracts.SQLExecutor; using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Infrastructure; namespace EnvelopeGenerator.Infrastructure;
public class SQLExecutor : ISQLExecutor public class SQLExecutor : ISQLExecutor
{ {
private readonly string _cnnStr = "Server=SDD-VMP04-SQL17\\DD_DEVELOP01;Database=DD_ECM;User Id=sa;Password=dd;Encrypt=false;TrustServerCertificate=True;"; private readonly SQLExecutorParams _params;
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public SQLExecutor(IServiceProvider provider) public SQLExecutor(IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions)
{ {
_provider = provider; _provider = provider;
_params = sqlExecutorParamsOptions.Value;
} }
public async Task<IEnumerable<TEntity>> Execute<TEntity>(string sql, DynamicParameters parameters, CancellationToken cancellation = default) public async Task<IEnumerable<TEntity>> Execute<TEntity>(string sql, DynamicParameters parameters, CancellationToken cancellation = default)
{ {
using var connection = new SqlConnection(_cnnStr); using var connection = new SqlConnection(_params.ConnectionString);
await connection.OpenAsync(cancellation); await connection.OpenAsync(cancellation);
return await connection.QueryAsync<TEntity>(sql, parameters); return await connection.QueryAsync<TEntity>(sql, parameters);
} }

View File

@ -1,6 +1,7 @@
using EnvelopeGenerator.Application.Contracts.SQLExecutor; using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Infrastructure; namespace EnvelopeGenerator.Infrastructure;
@ -10,7 +11,7 @@ public sealed class SQLExecutor<T> : SQLExecutor, ISQLExecutor<T> where T : clas
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public SQLExecutor(EGDbContext context, IServiceProvider provider) : base(provider) public SQLExecutor(EGDbContext context, IServiceProvider provider, IOptions<SQLExecutorParams> options) : base(provider, options)
{ {
_context = context; _context = context;
_provider = provider; _provider = provider;

View File

@ -0,0 +1,6 @@
namespace EnvelopeGenerator.Infrastructure;
public class SQLExecutorParams
{
public string? ConnectionString { get; set; }
}

View File

@ -27,11 +27,11 @@ public static class DependencyInjection
}); });
// Add envelope generator services // Add envelope generator services
services.AddEnvelopeGeneratorRepositories(options => options.UseSqlServer(connStr)); services.AddEnvelopeGeneratorInfrastructureServices(options => options.UseSqlServer(connStr));
return services return services
.AddSingleton<CommandManager>() .AddSingleton<CommandManager>()
.AddEnvelopeGeneratorRepositories() .AddEnvelopeGeneratorInfrastructureServices()
.AddEnvelopeGeneratorServices(configuration) .AddEnvelopeGeneratorServices(configuration)
.AddSingleton(sp => .AddSingleton(sp =>
{ {

View File

@ -17,7 +17,7 @@ public class Mock
builder.Configuration.AddJsonFile(configPath, optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile(configPath, optional: true, reloadOnChange: true);
builder.Services builder.Services
.AddEnvelopeGeneratorRepositories(opt => .AddEnvelopeGeneratorInfrastructureServices(opt =>
{ {
if (useRealDb) if (useRealDb)
{ {

View File

@ -90,7 +90,7 @@ try
}); });
// Add envelope generator services // Add envelope generator services
builder.Services.AddEnvelopeGeneratorRepositories(options => options.UseSqlServer(connStr)); builder.Services.AddEnvelopeGeneratorInfrastructureServices(options => options.UseSqlServer(connStr));
builder.Services.AddEnvelopeGeneratorServices(config); builder.Services.AddEnvelopeGeneratorServices(config);