Sidio.Mediator.SourceGenerator 2.0.6-preview

Prefix Reserved
This is a prerelease version of Sidio.Mediator.SourceGenerator.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Sidio.Mediator.SourceGenerator --version 2.0.6-preview
                    
NuGet\Install-Package Sidio.Mediator.SourceGenerator -Version 2.0.6-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.SourceGenerator" Version="2.0.6-preview">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sidio.Mediator.SourceGenerator" Version="2.0.6-preview" />
                    
Directory.Packages.props
<PackageReference Include="Sidio.Mediator.SourceGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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.SourceGenerator --version 2.0.6-preview
                    
#r "nuget: Sidio.Mediator.SourceGenerator, 2.0.6-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.SourceGenerator&version=2.0.6-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Sidio.Mediator.SourceGenerator&version=2.0.6-preview&prerelease
                    
Install as a Cake Tool

Sidio.Mediator

A simple implementation of the mediator pattern in .NET.

build Coverage Status

Core package

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.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 the IRequest or IHttpRequest implementations.
  • Register the IMediator service in your Startup.cs or Program.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> or IHttpRequest<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>
There are no supported framework assets in this 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 256 6/10/2025
2.0.6-preview 79 6/6/2025
2.0.5-preview 89 6/6/2025