Updated CreateEnvelopeCommandHandler to use IEnvelopeExecutor for managing envelope creation. Introduced CreateParmas method for parameter handling in CreateEnvelopeSQL. Corrected Dapper type mapping method name and added service registration for IEnvelopeExecutor in DependencyExtensions. Refactored SQLExecutor to enhance encapsulation. Created EnvelopeExecutor class implementing IEnvelopeExecutor for dedicated envelope operations.
33 lines
1.2 KiB
C#
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.Executor;
|
|
|
|
public class SQLExecutor : ISQLExecutor
|
|
{
|
|
protected readonly SQLExecutorParams Params;
|
|
|
|
protected 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);
|
|
}
|
|
} |