Sidio.Mediator
2.0.6-preview
Prefix Reserved
See the version list below for details.
dotnet add package Sidio.Mediator --version 2.0.6-preview
NuGet\Install-Package Sidio.Mediator -Version 2.0.6-preview
<PackageReference Include="Sidio.Mediator" Version="2.0.6-preview" />
<PackageVersion Include="Sidio.Mediator" Version="2.0.6-preview" />
<PackageReference Include="Sidio.Mediator" />
paket add Sidio.Mediator --version 2.0.6-preview
#r "nuget: Sidio.Mediator, 2.0.6-preview"
#addin nuget:?package=Sidio.Mediator&version=2.0.6-preview&prerelease
#tool nuget:?package=Sidio.Mediator&version=2.0.6-preview&prerelease
Sidio.Mediator
A simple implementation of the mediator pattern in .NET.
Core package
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.AddMediator(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" });
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.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.
The IMediator
implementation contains a method for each request. For example, for 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.SourceGenerator
in the project that contain theIRequest
orIHttpRequest
implementations. - Register the
IMediator
service in yourStartup.cs
orProgram.cs
:
services.AddMediatorService();
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. - Classes used in requests that are part of the parent namespace of that request should be included in global usings, e.g.:
<ItemGroup>
<Using Include="MyProject" />
</ItemGroup>
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 was computed. 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 was computed. 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. |
-
.NETStandard 2.0
- Scrutor (>= 6.0.1)
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.0.7-preview | 295 | 6/10/2025 |
2.0.6-preview | 108 | 6/6/2025 |
2.0.5-preview | 120 | 6/6/2025 |
1.0.7 | 188 | 5/23/2025 |