From 5331efe3c137e7dcf9c4a1d257cea886329c71b6 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 29 Apr 2025 16:39:18 +0200 Subject: [PATCH] =?UTF-8?q?feat(sql):=20Hinzuf=C3=BCgen=20generischer=20?= =?UTF-8?q?=C3=9Cberladungen=20zu=20SQLExecutor=20unter=20Verwendung=20von?= =?UTF-8?q?=20ISQL=20=C3=BCber=20DI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Es wurden Überladungen für ExecuteFirstAsync, ExecuteSingleAsync und ExecuteAllAsync eingeführt, die SQL-Definitionen aus dem Dependency Injection Container mittels ISQL auflösen. Außerdem wurde ein Fehler bei der Verwendung der Direktive von .cs zu standardmäßigem Schnittstellenimport korrigiert. --- .../Contracts/SQLExecutor/ISQL.cs | 20 ++++++++++++++++ .../Contracts/SQLExecutor/ISQLExecutor.cs | 6 +++++ .../SQLExecutor.cs | 23 ++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs new file mode 100644 index 00000000..00c0cd8d --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQL.cs @@ -0,0 +1,20 @@ +namespace EnvelopeGenerator.Application.Contracts.SQLExecutor; + +/// +/// Represents a raw SQL query contract. +/// +public interface ISQL +{ + /// + /// Gets the raw SQL query string. + /// + string Raw { get; } +} + +/// +/// Represents a typed SQL query contract for a specific entity. +/// +/// The type of the entity associated with the SQL query. +public interface ISQL : ISQL +{ +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs index e147218f..245657e9 100644 --- a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs @@ -32,4 +32,10 @@ public interface ISQLExecutor /// Optional parameters for the SQL query. /// An containing all matching results. Task> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters); + + Task ExecuteFirstAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL; + + Task ExecuteSingleAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL; + + Task> ExecuteAllAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL; } diff --git a/EnvelopeGenerator.Infrastructure/SQLExecutor.cs b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs index fdb69dd4..050beb28 100644 --- a/EnvelopeGenerator.Infrastructure/SQLExecutor.cs +++ b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs @@ -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 : ISQLExecutor 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 : ISQLExecutor where T : class .Set() .FromSqlRaw(sql, parameters) .ToListAsync(cancellation); + + public Task ExecuteFirstAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL + { + var sql = _provider.GetRequiredService(); + return ExecuteFirstAsync(sql.Raw); + } + + public Task ExecuteSingleAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL + { + var sql = _provider.GetRequiredService(); + return ExecuteSingleAsync(sql.Raw); + } + + public Task> ExecuteAllAsync(CancellationToken cancellation = default, params object[] parameters) where TSQL : ISQL + { + var sql = _provider.GetRequiredService(); + return ExecuteAllAsync(sql.Raw); + } }