Workleap.Extensions.MediatR 1.3.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Workleap.Extensions.MediatR --version 1.3.0                
NuGet\Install-Package Workleap.Extensions.MediatR -Version 1.3.0                
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="Workleap.Extensions.MediatR" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Workleap.Extensions.MediatR --version 1.3.0                
#r "nuget: Workleap.Extensions.MediatR, 1.3.0"                
#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.
// Install Workleap.Extensions.MediatR as a Cake Addin
#addin nuget:?package=Workleap.Extensions.MediatR&version=1.3.0

// Install Workleap.Extensions.MediatR as a Cake Tool
#tool nuget:?package=Workleap.Extensions.MediatR&version=1.3.0                

Workleap.Extensions.MediatR

nuget build

This library ensures that MediatR is registered in the dependency injection container as a singleton and also adds several features:

Getting started

Use the AddMediator(params Assembly[] assemblies) extension method on your dependency injection services (IServiceCollection) to automatically register all the MediatR request handlers from a given assembly.

builder.Services.AddMediator(typeof(Program).Assembly /*, [more assemblies...] */);

If you use Application Insights and want to instrument your handlers, you can install the dedicated NuGet package:

builder.Services.AddMediator(typeof(Program).Assembly).AddApplicationInsights();

There are multiple method overloads of AddMediator. For instance, you can override MediatR configuration using this overload that accepts a Action<MediatRServiceConfiguration>:

builder.Services.AddMediator(
    cfg => cfg.NotificationPublisher = new TaskWhenAllPublisher(),
    typeof(Program).Assembly);

Example

// CQRS naming conventions are suggested by a Roslyn analyzer, but it can be disabled
public sealed record SayHelloCommand([property: Required] string To) : IRequest;

public sealed class SayHelloCommandHandler : IRequestHandler<SayHelloCommand>
{
    public Task Handle(SayHelloCommand command, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Hello {command.To}!");
        return Task.CompletedTask;
    }
}

// [...] Retrieve an instance of IMediator or ISender
var mediator = serviceProvider.GetRequiredService<IMediator>();

// - We use the preferred Async-suffixed extension method to put emphasis on the asynchronous aspect of MediatR
// - A Roslyn analyzer suggests to specify a cancellation token, which is most of the time forgotten by developers
await mediator.SendAsync(new SayHelloCommand("world"), CancellationToken.None);

// This throws RequestValidationException because 'SayHelloCommand.To' is marked as required
await mediator.SendAsync(new SayHelloCommand(null!), CancellationToken.None);

Included Roslyn analyzers

Rule ID Category Severity Description
GMDTR01 Naming Warning Name should end with 'Command' or 'Query'
GMDTR02 Naming Warning Name should end with 'CommandHandler' or 'QueryHandler'
GMDTR03 Naming Warning Name should end with 'StreamQuery'
GMDTR04 Naming Warning Name should end with 'StreamQueryHandler'
GMDTR05 Naming Warning Name should end with 'Notification' or 'Event'
GMDTR06 Naming Warning Name should end with 'NotificationHandler' or 'EventHandler'
GMDTR07 Design Warning Use generic method instead
GMDTR08 Design Warning Provide a cancellation token
GMDTR09 Design Warning Handlers should not call other handlers
GMDTR10 Design Warning Handlers should not be public
GMDTR11 Design Warning Use 'AddMediator' extension method instead of 'AddMediatR'
GMDTR12 Design Warning Use method ending with 'Async' instead
GMDTR13 Naming Warning Name should end with 'Handler'

In order to change the severity of one of these diagnostic rules, use a .editorconfig file, for instance:

[*.cs]
dotnet_diagnostic.GMDTR01.severity = none

To learn more about how to configure or suppress code analysis warnings, read this documentation.

Building, releasing and versioning

The project can be built by running Build.ps1. It uses Microsoft.CodeAnalysis.PublicApiAnalyzers to help detect public API breaking changes. Use the built-in roslyn analyzer to ensure that public APIs are declared in PublicAPI.Shipped.txt, and obsolete public APIs in PublicAPI.Unshipped.txt.

A new preview NuGet package is automatically published on any new commit on the main branch. This means that by completing a pull request, you automatically get a new NuGet package.

When you are ready to officially release a stable NuGet package by following the SemVer guidelines, simply manually create a tag with the format x.y.z. This will automatically create and publish a NuGet package for this version.

License

Copyright © 2023, Workleap. This code is licensed under the Apache License, Version 2.0. You may obtain a copy of this license at https://github.com/gsoft-inc/gsoft-license/blob/master/LICENSE.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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. 
.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 (1)

Showing the top 1 NuGet packages that depend on Workleap.Extensions.MediatR:

Package Downloads
Workleap.Extensions.MediatR.ApplicationInsights

MediatR extensions, behaviors and default configuration.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.2-preview.13 23 11/29/2024
1.4.2-preview.12 35 11/24/2024
1.4.2-preview.11 34 11/16/2024
1.4.2-preview.10 43 10/31/2024
1.4.2-preview.9 37 10/30/2024
1.4.2-preview.8 40 10/28/2024
1.4.2-preview.7 41 10/28/2024
1.4.2-preview.6 39 10/28/2024
1.4.2-preview.5 44 10/7/2024
1.4.2-preview.4 48 10/1/2024
1.4.2-preview.3 46 10/1/2024
1.4.2-preview.1 49 9/27/2024
1.4.1 9,598 9/20/2024
1.4.1-preview.10 83 9/20/2024
1.4.1-preview.9 50 9/20/2024
1.4.1-preview.8 44 9/20/2024
1.4.1-preview.7 51 9/13/2024
1.4.1-preview.6 50 8/28/2024
1.4.1-preview.5 45 8/27/2024
1.4.1-preview.4 48 8/27/2024
1.4.1-preview.3 43 8/27/2024
1.4.1-preview.2 69 8/16/2024
1.4.1-preview.1 66 8/16/2024
1.4.0 5,164 7/31/2024
1.3.1-preview.20 36 7/31/2024
1.3.1-preview.19 59 7/15/2024
1.3.1-preview.18 40 7/5/2024
1.3.1-preview.17 68 6/10/2024
1.3.1-preview.16 58 5/31/2024
1.3.1-preview.15 57 5/31/2024
1.3.1-preview.14 58 5/28/2024
1.3.1-preview.13 55 5/21/2024
1.3.1-preview.12 75 5/10/2024
1.3.1-preview.11 77 4/12/2024
1.3.1-preview.10 72 4/3/2024
1.3.1-preview.9 69 4/3/2024
1.3.1-preview.8 68 3/8/2024
1.3.1-preview.7 58 3/7/2024
1.3.1-preview.6 64 3/4/2024
1.3.1-preview.5 81 3/1/2024
1.3.1-preview.4 65 2/16/2024
1.3.1-preview.3 66 2/12/2024
1.3.1-preview.2 65 2/6/2024
1.3.1-preview.1 67 2/6/2024
1.3.0 81,199 1/15/2024
1.2.1-preview.8 72 1/15/2024
1.2.1-preview.7 68 1/12/2024
1.2.1-preview.6 62 1/12/2024
1.2.1-preview.5 62 1/12/2024
1.2.1-preview.4 68 1/12/2024
1.2.1-preview.3 76 12/18/2023
1.2.1-preview.2 82 12/7/2023
1.2.1-preview.1 99 11/17/2023
1.2.0 18,252 11/15/2023
1.1.4-preview.14 69 11/14/2023
1.1.4-preview.13 71 11/13/2023
1.1.4-preview.12 70 11/13/2023
1.1.4-preview.11 83 11/6/2023
1.1.4-preview.10 76 10/30/2023
1.1.4-preview.9 74 10/20/2023
1.1.4-preview.8 83 10/17/2023
1.1.4-preview.7 88 10/13/2023
1.1.4-preview.6 81 9/26/2023
1.1.4-preview.5 74 9/25/2023
1.1.4-preview.4 79 9/21/2023
1.1.4-preview.3 76 9/20/2023
1.1.4-preview.2 72 9/20/2023
1.1.4-preview.1 74 9/20/2023
1.1.3 46,473 9/11/2023
1.1.3-preview.2 85 9/11/2023
1.1.3-preview.1 100 8/9/2023
1.1.2 232 7/20/2023
1.1.2-preview.7 104 7/20/2023