33 lines
1.2 KiB
C#
33 lines
1.2 KiB
C#
using EnvelopeGenerator.Application.Contracts.SQLExecutor.cs;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EnvelopeGenerator.Infrastructure;
|
|
|
|
public sealed class SQLExecutor<T> : ISQLExecutor<T> where T : class
|
|
{
|
|
private readonly EGDbContext _context;
|
|
|
|
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);
|
|
}
|