refactor: Vereinfachung der SQLExecutor-Implementierung zur Rückgabe von IQueryExecutor
- Aktualisiert `SQLExecutor<T>` um `ISQLExecutor<T>` zu implementieren und `IQueryExecutor<T>` für die weitere Abfrageausführung zurückzugeben. - Umstrukturierte Methoden zur Verwendung von `ToExecutor()` für die Konvertierung von rohen SQL-Abfragen in einen `IQueryExecutor<T>`. - Geänderter Konstruktor, um `IServiceProvider` für die Injektion von Abhängigkeiten von benutzerdefinierten SQL-Abfrageklassen zu akzeptieren.
This commit is contained in:
@@ -10,44 +10,21 @@ public sealed class SQLExecutor<T> : ISQLExecutor<T> where T : class
|
||||
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
public SQLExecutor(EGDbContext context)
|
||||
public SQLExecutor(EGDbContext context, IServiceProvider provider)
|
||||
{
|
||||
_context = context;
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public async Task<T?> ExecuteFirstAsync(string sql, CancellationToken cancellation = default, params object[] parameters)
|
||||
=> await _context
|
||||
.Set<T>()
|
||||
.FromSqlRaw(sql, parameters)
|
||||
.FirstOrDefaultAsync(cancellation);
|
||||
public IQueryExecutor<T> Execute(string sql, CancellationToken cancellation = default, params object[] parameters)
|
||||
=> _context
|
||||
.Set<T>()
|
||||
.FromSqlRaw(sql, parameters)
|
||||
.ToExecutor();
|
||||
|
||||
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>
|
||||
public IQueryExecutor<T> Execute<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);
|
||||
return Execute(sql.Raw);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user