33 lines
1.2 KiB
C#
33 lines
1.2 KiB
C#
using Dapper;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.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);
|
|
}
|
|
} |