From 7a0d4e2fa74b5df81a5985277dca86bf46f088c1 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 9 Apr 2026 10:25:59 +0200 Subject: [PATCH] Remove MediatorGetOrContext; add ExecuteAsync overloads Removed MediatorGetOrContext.cs, eliminating the fluent API for handling null or empty MediatR responses with custom exceptions. Added two ExecuteAsync overloads to FinalizeDocumentJob: one for processing a single EnvelopeDto and another for processing all envelopes with the EnvelopeCompletelySigned status. --- .../Common/Extensions/MediatorGetOrContext.cs | 77 ------------------- .../Jobs/FinalizeDocumentJob.cs | 2 + 2 files changed, 2 insertions(+), 77 deletions(-) delete mode 100644 EnvelopeGenerator.Application/Common/Extensions/MediatorGetOrContext.cs diff --git a/EnvelopeGenerator.Application/Common/Extensions/MediatorGetOrContext.cs b/EnvelopeGenerator.Application/Common/Extensions/MediatorGetOrContext.cs deleted file mode 100644 index f43cd0a3..00000000 --- a/EnvelopeGenerator.Application/Common/Extensions/MediatorGetOrContext.cs +++ /dev/null @@ -1,77 +0,0 @@ -using DigitalData.Core.Exceptions; -using MediatR; -using System.Collections; - -namespace EnvelopeGenerator.Application.Common.Extensions; - -/// -/// Holds a pending MediatR request and exposes Throw… methods that send the request -/// and throw a chosen exception when the response is null or an empty collection. -/// -/// The expected response type. -public readonly struct MediatorGetOrContext -{ - private readonly ISender _sender; - private readonly IRequest _request; - private readonly CancellationToken _cancel; - - internal MediatorGetOrContext(ISender sender, IRequest request, CancellationToken cancel) - { - _sender = sender; - _request = request; - _cancel = cancel; - } - - /// - /// Sends the request and throws the exception produced by - /// when the response is null or an empty collection. - /// - public async Task Throw(Func exceptionFactory) - { - if (await _sender.Send(_request, _cancel) is TResponse res) - { - // string implements IEnumerable, so "" would be treated as an empty collection without this guard. - if (res is not string && res is IEnumerable enumerable && !enumerable.Cast().Any()) - throw exceptionFactory(); - - return res; - } - - throw exceptionFactory(); - } - - /// - /// Sends the request and throws when the response is null or an empty collection. - /// - public Task ThrowNotFound(string? message = null) - => Throw(() => new NotFoundException(message ?? $"The requested resource of type {typeof(TResponse).Name} was not found.")); - - /// - /// Sends the request and throws when the response is null or an empty collection. - /// - public Task ThrowInvalidOperation(string? message = null) - => Throw(() => new InvalidOperationException(message ?? $"The operation for {typeof(TResponse).Name} returned no result.")); - - /// - /// Sends the request and throws when the response is null or an empty collection. - /// - public Task ThrowBadRequest(string? message = null) - => Throw(() => new BadRequestException(message ?? $"The request for {typeof(TResponse).Name} is invalid.")); -} - -/// -/// Extension methods for that provide a fluent API for enforcing non-null responses. -/// -public static class MediatorGetOrContext -{ - /// - /// Begins a fluent chain that sends and lets you choose how to handle a null or empty response. - /// Usage: - /// - /// await sender.GetOr(query).ThrowNotFound(); - /// await sender.GetOr(query, cancel).Throw(() => new MyException()); - /// - /// - public static MediatorGetOrContext GetOr(this ISender sender, IRequest request, CancellationToken cancel = default) - => new(sender, request, cancel); -} \ No newline at end of file diff --git a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs index 591ce5a4..285ef1d7 100644 --- a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs +++ b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs @@ -60,6 +60,8 @@ public class FinalizeDocumentJob(IOptions options, ILogger ExecuteAsync([envelope], cancel); + public async Task ExecuteAsync(CancellationToken cancel = default) { var envelopes = await mediator.Send(new ReadEnvelopeQuery()