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;
/// <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>
/// 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.
/// </summary>
/// <typeparam name="TResponse">The expected response type.</typeparam>
public readonly struct GetOrContext<TResponse>
public readonly struct MediatorGetOrContext<TResponse>
{
private readonly ISender _sender;
private readonly IRequest<TResponse?> _request;
private readonly CancellationToken _cancel;
internal GetOrContext(ISender sender, IRequest<TResponse?> request, CancellationToken cancel)
internal MediatorGetOrContext(ISender sender, IRequest<TResponse?> request, CancellationToken cancel)
{
_sender = sender;
_request = request;
@@ -74,4 +57,21 @@ public readonly struct GetOrContext<TResponse>
/// </summary>
public Task<TResponse> ThrowBadRequest(string? message = null)
=> 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);
}