CSharpEssentials.Mediator
3.0.4
See the version list below for details.
dotnet add package CSharpEssentials.Mediator --version 3.0.4
NuGet\Install-Package CSharpEssentials.Mediator -Version 3.0.4
<PackageReference Include="CSharpEssentials.Mediator" Version="3.0.4" />
<PackageVersion Include="CSharpEssentials.Mediator" Version="3.0.4" />
<PackageReference Include="CSharpEssentials.Mediator" />
paket add CSharpEssentials.Mediator --version 3.0.4
#r "nuget: CSharpEssentials.Mediator, 3.0.4"
#:package CSharpEssentials.Mediator@3.0.4
#addin nuget:?package=CSharpEssentials.Mediator&version=3.0.4
#tool nuget:?package=CSharpEssentials.Mediator&version=3.0.4
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, andIQueryHandlerare provided by theMediatorpackage itself. This package adds marker interfaces and pipeline behaviors that integrate withResult<T>.
Features
- Validation Behavior — FluentValidation integration; validation failures map to
Result.Failureautomatically. - Logging Behavior — Opt-in request/response payload logging with elapsed-time tracking.
- Caching Behavior —
IDistributedCacheintegration with bypass and failure-cache control. - Transaction Behavior — Wraps handlers in
TransactionScopewith async flow enabled.
Installation
dotnet add package CSharpEssentials.Mediator
dotnet add package Mediator.SourceGenerator --version 3.0.*
Mediator.SourceGeneratormust 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.ResultPatternCSharpEssentials.ErrorsCSharpEssentials.JsonFluentValidationMediator.Abstractions
| Product | Versions 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. |
-
.NETStandard 2.1
- CSharpEssentials.Errors (>= 3.0.4)
- CSharpEssentials.Json (>= 3.0.4)
- CSharpEssentials.Maybe (>= 3.0.4)
- CSharpEssentials.Results (>= 3.0.4)
- FluentValidation.DependencyInjectionExtensions (>= 11.0.0)
- Mediator.Abstractions (>= 3.0.1)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.4)
- System.Text.Json (>= 9.0.0)
-
net10.0
- CSharpEssentials.Errors (>= 3.0.4)
- CSharpEssentials.Json (>= 3.0.4)
- CSharpEssentials.Maybe (>= 3.0.4)
- CSharpEssentials.Results (>= 3.0.4)
- FluentValidation.DependencyInjectionExtensions (>= 11.0.0)
- Mediator.Abstractions (>= 3.0.1)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.4)
-
net11.0
- CSharpEssentials.Errors (>= 3.0.4)
- CSharpEssentials.Json (>= 3.0.4)
- CSharpEssentials.Maybe (>= 3.0.4)
- CSharpEssentials.Results (>= 3.0.4)
- FluentValidation.DependencyInjectionExtensions (>= 11.0.0)
- Mediator.Abstractions (>= 3.0.1)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.4)
-
net9.0
- CSharpEssentials.Errors (>= 3.0.4)
- CSharpEssentials.Json (>= 3.0.4)
- CSharpEssentials.Maybe (>= 3.0.4)
- CSharpEssentials.Results (>= 3.0.4)
- FluentValidation.DependencyInjectionExtensions (>= 11.0.0)
- Mediator.Abstractions (>= 3.0.1)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.4)
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.