GSoft.Extensions.MediatR
0.2.0
Prefix Reserved
See the version list below for details.
dotnet add package GSoft.Extensions.MediatR --version 0.2.0
NuGet\Install-Package GSoft.Extensions.MediatR -Version 0.2.0
<PackageReference Include="GSoft.Extensions.MediatR" Version="0.2.0" />
<PackageVersion Include="GSoft.Extensions.MediatR" Version="0.2.0" />
<PackageReference Include="GSoft.Extensions.MediatR" />
paket add GSoft.Extensions.MediatR --version 0.2.0
#r "nuget: GSoft.Extensions.MediatR, 0.2.0"
#:package GSoft.Extensions.MediatR@0.2.0
#addin nuget:?package=GSoft.Extensions.MediatR&version=0.2.0
#tool nuget:?package=GSoft.Extensions.MediatR&version=0.2.0
GSoft.Extensions.MediatR
This library ensures that MediatR is registered in the dependency injection container as a singleton and also adds several features:
- Activity-based OpenTelemetry instrumentation
- High-performance logging with
Debuglog level - Data annotations support for request validation, similar to ASP.NET Core model validation
- Application Insights instrumentation (in a separate NuGet package)
- CQRS conventions and MediatR best practices with Roslyn analyzers
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);
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();
Example
public sealed record SayHelloRequest([property: Required] string To) : IRequest<string>;
public sealed class SayHelloRequestHandler : IRequestHandler<SayHelloRequest, string>
{
public Task<string> Handle(SayHelloRequest request, CancellationToken cancellationToken)
{
return Task.FromResult($"Hello {request.To}!");
}
}
// [...]
var mediator = serviceProvider.GetRequiredService<IMediator>();
var result = await mediator.Send(new SayHelloRequest("world"));
// This throws RequestValidationException because SayHelloRequest.To is marked as required
await mediator.Send(new SayHelloRequest(null));
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 |
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, GSoft Group Inc. 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 | Versions 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. 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
- MediatR (>= 12.0.1)
- MediatR.Contracts (>= 2.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Diagnostics.DiagnosticSource (>= 6.0.0)
-
net6.0
- MediatR (>= 12.0.1)
- MediatR.Contracts (>= 2.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on GSoft.Extensions.MediatR:
| Package | Downloads |
|---|---|
|
GSoft.Extensions.MediatR.ApplicationInsights
MediatR extensions, behaviors and default configuration. |
GitHub repositories
This package is not used by any popular GitHub repositories.