Fluent.Unions.MediatR
1.1.0
dotnet add package Fluent.Unions.MediatR --version 1.1.0
NuGet\Install-Package Fluent.Unions.MediatR -Version 1.1.0
<PackageReference Include="Fluent.Unions.MediatR" Version="1.1.0" />
<PackageVersion Include="Fluent.Unions.MediatR" Version="1.1.0" />
<PackageReference Include="Fluent.Unions.MediatR" />
paket add Fluent.Unions.MediatR --version 1.1.0
#r "nuget: Fluent.Unions.MediatR, 1.1.0"
#:package Fluent.Unions.MediatR@1.1.0
#addin nuget:?package=Fluent.Unions.MediatR&version=1.1.0
#tool nuget:?package=Fluent.Unions.MediatR&version=1.1.0
![]()
Fluent.Unions.MediatR
Pipeline behaviors for MediatR that return a failed Result<T> instead of throwing. They build on Fluent.Unions.
License of MediatR
This package depends on MediatR 15, which is dual licensed: the Reciprocal Public License 1.5 (RPL-1.5) or a commercial license from Lucky Penny Software. Check that the license fits your project before you use it. Fluent.Unions.MediatR itself has the license of this repository. For a permissive alternative, see Fluent.Unions.Mediator.
Installation
dotnet add package Fluent.Unions.MediatR
Usage
Register the behaviors as open generics. The first one in the list is the outermost.
using Fluent.Unions.MediatR;
services.AddMediatR(configuration =>
{
configuration.RegisterServicesFromAssemblyContaining<Program>();
configuration.AddOpenBehavior(typeof(ExceptionToResultBehavior<,>));
configuration.AddOpenBehavior(typeof(ValidationToResultBehavior<,>));
});
A request whose response is a Result<T> now fails with an error instead of throwing.
public sealed record CreateUser([property: Required] string Name, [property: Range(1, 120)] int Age)
: IRequest<Result<UserId>>;
Result<UserId> result = await sender.Send(new CreateUser("", 0));
// error: ValidationErrors for "Name" and "Age"; the handler did not run
ExceptionToResultBehavior<TRequest, TResponse>returns an exception thrown by the handler or a later behavior as the error.OperationCanceledExceptionstill throws.ValidationToResultBehavior<TRequest, TResponse>validates the request with itsSystem.ComponentModel.DataAnnotationsattributes andIValidatableObject, and returnsValidationErrors. Put[property: ...]on record parameters.
With your own result type
TResponse can be any result type: Result, Result<T> or your own declared result. The error is what the response type's FromException makes of the exception or of the ValidationErrors. A request with another response type is left alone.
The behaviors use reflection, so they are not trimming or AOT friendly. See your own result type.
Documentation
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Fluent.Unions (>= 1.1.0)
- MediatR (>= 15.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.