Sidio.Mediator
2.1.3
Prefix Reserved
dotnet add package Sidio.Mediator --version 2.1.3
NuGet\Install-Package Sidio.Mediator -Version 2.1.3
<PackageReference Include="Sidio.Mediator" Version="2.1.3" />
<PackageVersion Include="Sidio.Mediator" Version="2.1.3" />
<PackageReference Include="Sidio.Mediator" />
paket add Sidio.Mediator --version 2.1.3
#r "nuget: Sidio.Mediator, 2.1.3"
#:package Sidio.Mediator@2.1.3
#addin nuget:?package=Sidio.Mediator&version=2.1.3
#tool nuget:?package=Sidio.Mediator&version=2.1.3
Sidio.Mediator
A simple implementation of the mediator pattern in .NET.
Core package (request abstractions)
Request validation package
Source generation for the Mediator service
Usage
Requests and requests handlers
// Define a request and request handler
public class MyRequest : IRequest<string>
{
public string Name { get; init; }
}
public class MyRequestHandler : IRequestHandler<MyRequest, string>
{
public Task<Result<string>> HandleAsync(MyRequest request, CancellationToken cancellationToken = default)
{
var result = Result<string>.Success($"Hello {request.Name}");
// Or: Result<string>.Failure("error code", "error message");
return Task.FromResult(result);
}
}
// Provide an arbitrary type to register all request handlers in the assembly of the type:
services.AddMediatorRequestHandlers(typeof(MyRequest));
// Get the request handler from the service provider
var requestHander = serviceProvider.GetRequiredService<IRequestHandler<MyRequest, string>>();
var result = await requestHander.HandleAsync(new MyRequest { Name = "World" });
// or use dependency injection
public class MyClass
{
public MyClass(IRequestHandler<MyRequest, string> requestHandler)
{
}
}
Http requests
// Define a request and request handler
public class MyHttpRequest : IHttpRequest<string>
{
public string Name { get; init; }
}
public class MyHttpRequestHandler : IHttpRequestHandler<MyRequest, string>
{
public Task<HttpResult<string>> HandleAsync(MyRequest request, CancellationToken cancellationToken = default)
{
var result = HttpResult<string>.Ok($"Hello {request.Name}");
// Or for example: HttpResult<string>.Unauthorized();
return Task.FromResult(result);
}
}
Request validation
Request validation uses FluentValidation.
// Define a validator
public class MyRequestValidator : AbstractValidator<MyRequest>
{
public MyRequestValidator()
{
RuleFor(x => x.Name).NotEmpty();
}
}
// Provide an arbitrary type to register all validators in the assembly of the type:
services
.AddMediatorRequestHandlers(typeof(MyRequest))
.AddMediatorValidation(typeof(MyRequest));
Source generators (v2.0+)
In version 2.0 and later, Sidio.Mediator.SourceGenerator includes source generators that create an IMediator service implementation at
compile time. This service works both with requests and request validation.
The IMediator implementation contains a method for each request. For example, a request named MyRequest:
public class MyRequest : IRequest<string>;
The generated IMediator will have a method:
Task<Result<string>> MyRequestAsync(MyRequest request, CancellationToken cancellationToken = default);
Setup
- Add package reference to
Sidio.Mediator.SourceGeneratorin the project that contain theIRequestorIHttpRequestimplementations. - Register the
IMediatorservice in yourStartup.csorProgram.cs:
services
.AddMediatorRequestHandlers(typeof(MyRequest)) // register the request handlers
.AddMediatorValidation(typeof(MyRequest)) // register the request validators
.AddMediatorService(); // register the IMediator service
Limitations
- Requests should have a unique name across the project which implements the source generator.
- Requests should not be nested in other classes.
- Requests should always implement
IRequest,IRequest<T>orIHttpRequest<T>. Inheritance of base/abstract requests is not supported. Inheritance of request handlers should work. - Types used in requests that are part of the parent namespace of that request should be included in global usings, e.g. in the csproj file:
<ItemGroup>
<Using Include="MyNamespace" />
</ItemGroup>
- The IMediator service will be located in namespace
Sidio.Mediator.
| 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Sidio.Mediator:
| Package | Downloads |
|---|---|
|
Sidio.Mediator.Validation
Validation features for Sidio.Mediator. |
|
|
Sidio.Mediator.SourceGenerator
A source generator for a centralized IMediator implementation. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 2.1.3 | 180 | 2/11/2026 | |
| 2.1.1 | 784 | 12/3/2025 | |
| 2.1.0 | 381 | 11/12/2025 | |
| 2.0.9 | 342 | 7/2/2025 | |
| 2.0.8-preview | 338 | 6/27/2025 | |
| 2.0.7-preview | 501 | 6/10/2025 | |
| 2.0.6-preview | 333 | 6/6/2025 | |
| 2.0.5-preview | 354 | 6/6/2025 | |
| 1.0.7 | 399 | 5/23/2025 |