Sidio.Mediator.Validation 2.0.7-preview

Prefix Reserved
This is a prerelease version of Sidio.Mediator.Validation.
dotnet add package Sidio.Mediator.Validation --version 2.0.7-preview
                    
NuGet\Install-Package Sidio.Mediator.Validation -Version 2.0.7-preview
                    
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="Sidio.Mediator.Validation" Version="2.0.7-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sidio.Mediator.Validation" Version="2.0.7-preview" />
                    
Directory.Packages.props
<PackageReference Include="Sidio.Mediator.Validation" />
                    
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 Sidio.Mediator.Validation --version 2.0.7-preview
                    
#r "nuget: Sidio.Mediator.Validation, 2.0.7-preview"
                    
#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.
#addin nuget:?package=Sidio.Mediator.Validation&version=2.0.7-preview&prerelease
                    
Install Sidio.Mediator.Validation as a Cake Addin
#tool nuget:?package=Sidio.Mediator.Validation&version=2.0.7-preview&prerelease
                    
Install Sidio.Mediator.Validation as a Cake Tool

Sidio.Mediator

A simple implementation of the mediator pattern in .NET.

build Coverage Status

Core package (CQRS abstractions)

NuGet Version

Request validation package

NuGet Version

Source generation for the Mediator service

NuGet Version

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.AddMediatorCqrs(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
    .AddMediatorCqrs(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.SourceGenerator in the project that contain the IRequest or IHttpRequest implementations.
  • Register the IMediator service in your Startup.cs or Program.cs:
services
    .AddMediatorCqrs(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> or IHttpRequest<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 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.7-preview 270 6/10/2025
2.0.6-preview 95 6/6/2025
2.0.5-preview 100 6/6/2025
1.0.7 182 5/23/2025