CSharpEssentials.Mediator 3.0.5

dotnet add package CSharpEssentials.Mediator --version 3.0.5
                    
NuGet\Install-Package CSharpEssentials.Mediator -Version 3.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharpEssentials.Mediator" Version="3.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Mediator" Version="3.0.5" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Mediator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharpEssentials.Mediator --version 3.0.5
                    
#r "nuget: CSharpEssentials.Mediator, 3.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CSharpEssentials.Mediator@3.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSharpEssentials.Mediator&version=3.0.5
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Mediator&version=3.0.5
                    
Install as a Cake Tool

CSharpEssentials.Mediator

Pipeline behaviors for the Mediator source-generator library, with built-in support for Result<T>, validation, caching, logging, and transactions. Native AOT compatible — zero reflection at runtime.

Note: ICommand, IQuery, ICommandHandler, and IQueryHandler are provided by the Mediator package itself. This package adds marker interfaces and pipeline behaviors that integrate with Result<T>.

Features

  • Validation Behavior — FluentValidation integration; validation failures map to Result.Failure automatically.
  • Logging Behavior — Opt-in request/response payload logging with elapsed-time tracking.
  • Caching BehaviorIDistributedCache integration with bypass and failure-cache control.
  • Transaction Behavior — Wraps handlers in TransactionScope with async flow enabled.

Installation

dotnet add package CSharpEssentials.Mediator
dotnet add package Mediator.SourceGenerator --version 3.0.*

Mediator.SourceGenerator must be referenced by your entry project.

Usage

Registration

Standard (non-AOT)

services.AddMediatorBehaviors(); // Registers all 4 behaviors

// Or individually:
services.AddMediatorValidationBehavior();
services.AddMediatorLoggingBehavior();
services.AddMediatorCachingBehavior();
services.AddMediatorTransactionBehavior();

Native AOT

When targeting Native AOT, pass behaviors via AddMediator options instead of relying on open-generic DI registration. DefaultPipelineBehaviors provides the pre-ordered array:

services.AddMediator(options =>
{
    options.PipelineBehaviors = CSharpEssentials.Mediator.MediatorExtensions.DefaultPipelineBehaviors;
});

Pipeline order: Validation → Logging → Caching → Transaction

See Mediator §4.4 — Use pipeline behaviors for the full Native AOT pipeline behavior documentation.

Commands & Queries

Use ICommand, IQuery, ICommandHandler, and IQueryHandler directly from the Mediator package. Return Result<T> from handlers naturally.

using Mediator;
using CSharpEssentials.ResultPattern;

// Unit command
public record DeleteUserCommand(Guid Id) : ICommand<Result>;

public class DeleteUserCommandHandler : ICommandHandler<DeleteUserCommand, Result>
{
    public async ValueTask<Result> Handle(DeleteUserCommand command, CancellationToken ct)
    {
        await _repo.DeleteAsync(command.Id, ct);
        return Result.Success();
    }
}

// Command with response
public record CreateUserCommand(string Email, string Name) : ICommand<Result<Guid>>;

public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand, Result<Guid>>
{
    public async ValueTask<Result<Guid>> Handle(CreateUserCommand command, CancellationToken ct)
    {
        var user = new User { Email = command.Email, Name = command.Name };
        await _repo.AddAsync(user, ct);
        return user.Id;
    }
}

// Query
public record GetUserQuery(Guid Id) : IQuery<Result<User>>;

public class GetUserQueryHandler : IQueryHandler<GetUserQuery, Result<User>>
{
    public async ValueTask<Result<User>> Handle(GetUserQuery query, CancellationToken ct)
    {
        var user = await _repo.FindAsync(query.Id, ct);
        return user ?? Result.Failure<User>(Error.NotFound("User.NotFound", "User not found"));
    }
}

Validation Behavior

Add FluentValidation validators to DI. The behavior intercepts requests before the handler runs.

public class CreateUserCommandValidator : AbstractValidator<CreateUserCommand>
{
    public CreateUserCommandValidator()
    {
        RuleFor(x => x.Email).NotEmpty().EmailAddress();
        RuleFor(x => x.Name).NotEmpty().MaximumLength(100);
    }
}

Validation errors are converted to Error.Validation with PropertyName preserved in metadata. Multiple validators are aggregated.

Caching Behavior

Implement ICacheable on any request.

public record GetUserQuery(Guid Id) : IQuery<Result<User>>, ICacheable
{
    public string CacheKey => $"user:{Id}";
    public TimeSpan Expiration => TimeSpan.FromMinutes(5);
    public bool BypassCache => false;
    public bool CacheFailures => false;
}
Property Purpose
CacheKey Unique key for the cached entry
Expiration Absolute expiration relative to now
BypassCache When true, skips cache lookup and stores fresh
CacheFailures When true, caches failed results too

Requires IDistributedCache registered in DI.

Transaction Scope Behavior

Implement ITransactionalRequest on any command.

public record TransferMoneyCommand(Guid From, Guid To, decimal Amount)
    : ICommand<Result>, ITransactionalRequest;

The handler is wrapped in a TransactionScope with TransactionScopeAsyncFlowOption.Enabled.

Logging Behavior

Implement marker interfaces to opt into payload logging.

public record GetUserQuery(Guid Id) : IQuery<Result<User>>, IRequestResponseLoggable;
Interface Logs
IRequestLoggable Request payload as JSON
IResponseLoggable Response payload as JSON
IRequestResponseLoggable Both request and response

All requests log basic handling/handled messages and elapsed time regardless of marker interfaces.

Marker Interfaces

Interface Extends Purpose
ICacheable Enables CachingBehavior
ITransactionalRequest Enables TransactionScopeBehavior
ILoggableRequest IMessage Base for logging opt-in
IRequestLoggable ILoggableRequest Log request payload
IResponseLoggable ILoggableRequest Log response payload
IRequestResponseLoggable IRequestLoggable, IResponseLoggable Log both

Pipeline Behaviors

Behavior Trigger Constraint
ValidationBehavior<TRequest,TResponse> Always registered TRequest : IMessage
LoggingBehavior<TRequest,TResponse> Always registered TRequest : ILoggableRequest, IMessage
CachingBehavior<TRequest,TResponse> Always registered TRequest : ICacheable, IMessage
TransactionScopeBehavior<TRequest,TResponse> Always registered TRequest : ITransactionalRequest, IMessage

Behaviors are registered as singletons for IPipelineBehavior<,>. The Mediator source-generator automatically includes them in the pipeline for matching request types.

Dependencies

  • CSharpEssentials.ResultPattern
  • CSharpEssentials.Errors
  • CSharpEssentials.Json
  • FluentValidation
  • Mediator.Abstractions
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on CSharpEssentials.Mediator:

Package Downloads
CSharpEssentials

A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.5 0 5/7/2026
3.0.4 31 5/6/2026
3.0.3 31 5/5/2026