Developer 02 a42e4287ff 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.
2025-05-05 10:47:00 +02:00

33 lines
1.2 KiB
C#

using Dapper;
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace EnvelopeGenerator.Infrastructure;
public class SQLExecutor : ISQLExecutor
{
private readonly SQLExecutorParams _params;
private readonly IServiceProvider _provider;
public SQLExecutor(IServiceProvider provider, IOptions<SQLExecutorParams> sqlExecutorParamsOptions)
{
_provider = provider;
_params = sqlExecutorParamsOptions.Value;
}
public async Task<IEnumerable<TEntity>> Execute<TEntity>(string sql, DynamicParameters parameters, CancellationToken cancellation = default)
{
using var connection = new SqlConnection(_params.ConnectionString);
await connection.OpenAsync(cancellation);
return await connection.QueryAsync<TEntity>(sql, parameters);
}
public Task<IEnumerable<TEntity>> Execute<TEntity, TSQL>(DynamicParameters parameters, CancellationToken cancellation = default) where TSQL : ISQL
{
var sql = _provider.GetRequiredService<TSQL>();
return Execute<TEntity>(sql.Raw, parameters, cancellation);
}
}