feat(sql): Hinzufügen generischer Überladungen zu SQLExecutor unter Verwendung von ISQL<T> über DI
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.
This commit is contained in:
parent
3b4ad2960a
commit
5331efe3c1
20
EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs
Normal file
20
EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace EnvelopeGenerator.Application.Contracts.SQLExecutor;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a raw SQL query contract.
|
||||
/// </summary>
|
||||
public interface ISQL
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the raw SQL query string.
|
||||
/// </summary>
|
||||
string Raw { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a typed SQL query contract for a specific entity.
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">The type of the entity associated with the SQL query.</typeparam>
|
||||
public interface ISQL<TEntity> : ISQL
|
||||
{
|
||||
}
|
||||
@ -32,4 +32,10 @@ public interface ISQLExecutor<TEntity>
|
||||
/// <param name="parameters">Optional parameters for the SQL query.</param>
|
||||
/// <returns>An <see cref="IEnumerable{TEntity}"/> containing all matching results.</returns>
|
||||
Task<IEnumerable<TEntity>> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters);
|
||||
|
||||
Task<TEntity?> ExecuteFirstAsync<TSQL>(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL<TEntity>;
|
||||
|
||||
Task<TEntity?> ExecuteSingleAsync<TSQL>(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL<TEntity>;
|
||||
|
||||
Task<IEnumerable<TEntity>> ExecuteAllAsync<TSQL>(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL<TEntity>;
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using EnvelopeGenerator.Application.Contracts.SQLExecutor.cs;
|
||||
using EnvelopeGenerator.Application.Contracts.SQLExecutor;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace EnvelopeGenerator.Infrastructure;
|
||||
|
||||
@ -7,6 +8,8 @@ public sealed class SQLExecutor<T> : ISQLExecutor<T> where T : class
|
||||
{
|
||||
private readonly EGDbContext _context;
|
||||
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
public SQLExecutor(EGDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
@ -29,4 +32,22 @@ public sealed class SQLExecutor<T> : ISQLExecutor<T> where T : class
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user