Refactor Mediator GetOr API for naming consistency

Renamed MediatorExtensions to MediatorGetOrContext and GetOrContext<TResponse> to MediatorGetOrContext<TResponse> for consistent naming. Moved the GetOr extension method into the new static class. Updated XML docs and reorganized declarations; no functional changes.
This commit is contained in:
2026-04-08 15:34:39 +02:00
parent 9bdf24d7d5
commit 3955ee9f39

View File

@@ -4,35 +4,18 @@ using System.Collections;
namespace EnvelopeGenerator.Application.Common.Extensions; namespace EnvelopeGenerator.Application.Common.Extensions;
/// <summary>
/// Extension methods for <see cref="ISender"/> that provide a fluent API for enforcing non-null responses.
/// </summary>
public static class MediatorExtensions
{
/// <summary>
/// Begins a fluent chain that sends <paramref name="request"/> and lets you choose how to handle a <c>null</c> or empty response.
/// <para>Usage:</para>
/// <code>
/// await sender.GetOr(query).ThrowNotFound();
/// await sender.GetOr(query, cancel).Throw(() => new MyException());
/// </code>
/// </summary>
public static GetOrContext<TResponse> GetOr<TResponse>(this ISender sender, IRequest<TResponse?> request, CancellationToken cancel = default)
=> new(sender, request, cancel);
}
/// <summary> /// <summary>
/// Holds a pending MediatR request and exposes <c>Throw…</c> methods that send the request /// Holds a pending MediatR request and exposes <c>Throw…</c> methods that send the request
/// and throw a chosen exception when the response is <c>null</c> or an empty collection. /// and throw a chosen exception when the response is <c>null</c> or an empty collection.
/// </summary> /// </summary>
/// <typeparam name="TResponse">The expected response type.</typeparam> /// <typeparam name="TResponse">The expected response type.</typeparam>
public readonly struct GetOrContext<TResponse> public readonly struct MediatorGetOrContext<TResponse>
{ {
private readonly ISender _sender; private readonly ISender _sender;
private readonly IRequest<TResponse?> _request; private readonly IRequest<TResponse?> _request;
private readonly CancellationToken _cancel; private readonly CancellationToken _cancel;
internal GetOrContext(ISender sender, IRequest<TResponse?> request, CancellationToken cancel) internal MediatorGetOrContext(ISender sender, IRequest<TResponse?> request, CancellationToken cancel)
{ {
_sender = sender; _sender = sender;
_request = request; _request = request;
@@ -75,3 +58,20 @@ public readonly struct GetOrContext<TResponse>
public Task<TResponse> ThrowBadRequest(string? message = null) public Task<TResponse> ThrowBadRequest(string? message = null)
=> Throw(() => new BadRequestException(message ?? $"The request for {typeof(TResponse).Name} is invalid.")); => Throw(() => new BadRequestException(message ?? $"The request for {typeof(TResponse).Name} is invalid."));
} }
/// <summary>
/// Extension methods for <see cref="ISender"/> that provide a fluent API for enforcing non-null responses.
/// </summary>
public static class MediatorGetOrContext
{
/// <summary>
/// Begins a fluent chain that sends <paramref name="request"/> and lets you choose how to handle a <c>null</c> or empty response.
/// <para>Usage:</para>
/// <code>
/// await sender.GetOr(query).ThrowNotFound();
/// await sender.GetOr(query, cancel).Throw(() => new MyException());
/// </code>
/// </summary>
public static MediatorGetOrContext<TResponse> GetOr<TResponse>(this ISender sender, IRequest<TResponse?> request, CancellationToken cancel = default)
=> new(sender, request, cancel);
}