Es wurden Überladungen für ExecuteFirstAsync, ExecuteSingleAsync und ExecuteAllAsync eingeführt, die SQL-Definitionen aus dem Dependency Injection Container mittels ISQL<T> auflösen. Außerdem wurde ein Fehler bei der Verwendung der Direktive von .cs zu standardmäßigem Schnittstellenimport korrigiert.
54 lines
2.0 KiB
C#
54 lines
2.0 KiB
C#
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace EnvelopeGenerator.Infrastructure;
|
|
|
|
public sealed class SQLExecutor<T> : ISQLExecutor<T> where T : class
|
|
{
|
|
private readonly EGDbContext _context;
|
|
|
|
private readonly IServiceProvider _provider;
|
|
|
|
public SQLExecutor(EGDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<T?> ExecuteFirstAsync(string sql, CancellationToken cancellation = default, params object[] parameters)
|
|
=> await _context
|
|
.Set<T>()
|
|
.FromSqlRaw(sql, parameters)
|
|
.FirstOrDefaultAsync(cancellation);
|
|
|
|
public async Task<T?> ExecuteSingleAsync(string sql, CancellationToken cancellation = default, params object[] parameters)
|
|
=> await _context
|
|
.Set<T>()
|
|
.FromSqlRaw(sql, parameters)
|
|
.SingleOrDefaultAsync(cancellation);
|
|
|
|
public async Task<IEnumerable<T>> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters)
|
|
=> await _context
|
|
.Set<T>()
|
|
.FromSqlRaw(sql, parameters)
|
|
.ToListAsync(cancellation);
|
|
|
|
public Task<T?> ExecuteFirstAsync<TSQL>(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL<T>
|
|
{
|
|
var sql = _provider.GetRequiredService<TSQL>();
|
|
return ExecuteFirstAsync(sql.Raw);
|
|
}
|
|
|
|
public Task<T?> ExecuteSingleAsync<TSQL>(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL<T>
|
|
{
|
|
var sql = _provider.GetRequiredService<TSQL>();
|
|
return ExecuteSingleAsync(sql.Raw);
|
|
}
|
|
|
|
public Task<IEnumerable<T>> ExecuteAllAsync<TSQL>(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL<T>
|
|
{
|
|
var sql = _provider.GetRequiredService<TSQL>();
|
|
return ExecuteAllAsync(sql.Raw);
|
|
}
|
|
}
|