Fluent.Unions.Mediator
1.1.0
dotnet add package Fluent.Unions.Mediator --version 1.1.0
NuGet\Install-Package Fluent.Unions.Mediator -Version 1.1.0
<PackageReference Include="Fluent.Unions.Mediator" Version="1.1.0" />
<PackageVersion Include="Fluent.Unions.Mediator" Version="1.1.0" />
<PackageReference Include="Fluent.Unions.Mediator" />
paket add Fluent.Unions.Mediator --version 1.1.0
#r "nuget: Fluent.Unions.Mediator, 1.1.0"
#:package Fluent.Unions.Mediator@1.1.0
#addin nuget:?package=Fluent.Unions.Mediator&version=1.1.0
#tool nuget:?package=Fluent.Unions.Mediator&version=1.1.0
![]()
Fluent.Unions.Mediator
Pipeline behaviors for the source-generated Mediator library (MIT) that return a failed Result<T> instead of throwing. They build on Fluent.Unions and work with requests whose response is a C# 15 union.
Installation
dotnet add package Fluent.Unions.Mediator
The package depends on Mediator.Abstractions. Your application also references Mediator.SourceGenerator, as Mediator requires.
Usage
Register the behaviors as open generics. The first one in the list is the outermost.
using Fluent.Unions.Mediator;
services.AddMediator(options =>
{
options.PipelineBehaviors = [typeof(ExceptionToResultBehavior<,>), 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 mediator.Send(new CreateUser("", 0));
// error: ValidationErrors for "Name" and "Age"; the handler did not run
ExceptionToResultBehavior<TMessage, TResponse>returns an exception thrown by the handler or a later behavior as the error.OperationCanceledExceptionstill throws.ValidationToResultBehavior<TMessage, 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.
A request that returns the value-less result needs it declared explicitly next to Result<T>, because the Mediator source generator cannot see types that another generator emits.
[FluentResult<DomainException>]
public readonly partial struct Result<T>;
[FluentResult<DomainException>]
public readonly partial struct Result;
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)
- Mediator.Abstractions (>= 3.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.