From 3b4ad2960a7134df184103d881205921a7ab51c2 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 29 Apr 2025 16:26:26 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20SQLExecutor-Klasse=20f=C3=BCr=20die=20A?= =?UTF-8?q?usf=C3=BChrung=20von=20Roh-SQL-Abfragen=20mit=20Entity=20Framew?= =?UTF-8?q?ork=20Core=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Contracts/SQLExecutor/ISQLExecutor.cs | 35 +++++++++++++++++++ .../EnvelopeGenerator.Application.csproj | 4 +++ .../SQLExecutor.cs | 32 +++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs create mode 100644 EnvelopeGenerator.Infrastructure/SQLExecutor.cs diff --git a/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs new file mode 100644 index 00000000..e147218f --- /dev/null +++ b/EnvelopeGenerator.Application/Contracts/SQLExecutor/ISQLExecutor.cs @@ -0,0 +1,35 @@ +namespace EnvelopeGenerator.Application.Contracts.SQLExecutor; + +/// +/// Defines methods for executing raw SQL queries and mapping the results to objects. +/// +/// The entity type to which the SQL query results will be mapped. +public interface ISQLExecutor +{ + /// + /// Executes a raw SQL query and returns the first result or null if no result is found. + /// + /// The raw SQL query to execute. + /// Optional cancellation token. + /// Optional parameters for the SQL query. + /// The first result, or null if none found. + Task ExecuteFirstAsync(string sql, CancellationToken cancellation = default, params object[] parameters); + + /// + /// Executes a raw SQL query and expects a single result, or null if no result is found. + /// + /// The raw SQL query to execute. + /// Optional cancellation token. + /// Optional parameters for the SQL query. + /// The single result, or null if none found. + Task ExecuteSingleAsync(string sql, CancellationToken cancellation = default, params object[] parameters); + + /// + /// Executes a raw SQL query and returns all results as a collection of . + /// + /// The raw SQL query to execute. + /// Optional cancellation token. + /// Optional parameters for the SQL query. + /// An containing all matching results. + Task> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters); +} diff --git a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj index 73ba45fe..0406d46a 100644 --- a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj +++ b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj @@ -80,4 +80,8 @@ + + + + diff --git a/EnvelopeGenerator.Infrastructure/SQLExecutor.cs b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs new file mode 100644 index 00000000..fdb69dd4 --- /dev/null +++ b/EnvelopeGenerator.Infrastructure/SQLExecutor.cs @@ -0,0 +1,32 @@ +using EnvelopeGenerator.Application.Contracts.SQLExecutor.cs; +using Microsoft.EntityFrameworkCore; + +namespace EnvelopeGenerator.Infrastructure; + +public sealed class SQLExecutor : ISQLExecutor where T : class +{ + private readonly EGDbContext _context; + + public SQLExecutor(EGDbContext context) + { + _context = context; + } + + public async Task ExecuteFirstAsync(string sql, CancellationToken cancellation = default, params object[] parameters) + => await _context + .Set() + .FromSqlRaw(sql, parameters) + .FirstOrDefaultAsync(cancellation); + + public async Task ExecuteSingleAsync(string sql, CancellationToken cancellation = default, params object[] parameters) + => await _context + .Set() + .FromSqlRaw(sql, parameters) + .SingleOrDefaultAsync(cancellation); + + public async Task> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters) + => await _context + .Set() + .FromSqlRaw(sql, parameters) + .ToListAsync(cancellation); +}