PlainBytes.Mediation.Mediator
0.1.0
dotnet add package PlainBytes.Mediation.Mediator --version 0.1.0
NuGet\Install-Package PlainBytes.Mediation.Mediator -Version 0.1.0
<PackageReference Include="PlainBytes.Mediation.Mediator" Version="0.1.0" />
<PackageVersion Include="PlainBytes.Mediation.Mediator" Version="0.1.0" />
<PackageReference Include="PlainBytes.Mediation.Mediator" />
paket add PlainBytes.Mediation.Mediator --version 0.1.0
#r "nuget: PlainBytes.Mediation.Mediator, 0.1.0"
#:package PlainBytes.Mediation.Mediator@0.1.0
#addin nuget:?package=PlainBytes.Mediation.Mediator&version=0.1.0
#tool nuget:?package=PlainBytes.Mediation.Mediator&version=0.1.0
PlainBytes.Mediation
YAMI (Yet another mediator implementation). Opinionated Mediator implementation to serve the authors needs. With the aim to keep it free and open-source.
Getting Started
Installation
Add the NuGet package to your project (replace with actual package name if published):
dotnet add package PlainBytes.Mediation.Mediator
Usage
1. Setup Dependency Injection and Mediator
var services = new ServiceCollection();
services.AddMediator();
2. Register Handlers
Register your command, query, and notification handlers:
services.AddTransient<IRequestHandler<CreateUserCommand>, CreateUserCommandHandler>();
services.AddTransient<IRequestHandler<CreateUserWithIdCommand, int>, CreateUserWithIdCommandHandler>();
services.AddTransient<IRequestHandler<GetUserNameQuery, string>, GetUserNameQueryHandler>();
services.AddTransient<IRequestHandler<FailingQuery, string>, FailingQueryHandler>();
services.AddTransient<INotificationHandler<UserCreatedNotification>, SendToastNotificationHandler>();
services.AddTransient<INotificationHandler<UserCreatedNotification>, SendWelcomeEmailNotificationHandler>();
3. Build Service Provider and Get Mediator
var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();
4. Example Usages
Send a command (no return value):
await mediator.Send(new CreateUserCommand { UserName = "Alice" });
Send a command (with return value):
var userId = await mediator.Send(new CreateUserWithIdCommand { UserName = "Bob" });
Console.WriteLine($"Returned user id: {userId}");
Execute a query:
var userName = await mediator.Get(new GetUserNameQuery { UserId = userId });
Console.WriteLine($"Fetched user name: {userName}");
Publish a notification:
await mediator.Publish(new UserCreatedNotification { UserName = userName });
5. RequestResult Examples
TrySend and TryGet return a RequestResult:
var nameResult = await mediator.TrySend(new CreateUserCommand { UserName = "Charlie" });
Console.WriteLine($"TrySend result: {nameResult}");
var idResult = await mediator.TrySend(new CreateUserWithIdCommand { UserName = "Diana" });
Console.WriteLine($"TrySend result: {idResult}");
var userNameResult = await mediator.TryGet(new GetUserNameQuery { UserId = 42 });
Console.WriteLine($"TryGet result: {userNameResult}");
var failedResult = await mediator.TryGet(new FailingQuery());
Console.WriteLine($"TryGet failed result: {failedResult}");
6. Adding Logging Pipeline Behaviors
You can enable built-in logging behaviors for requests and notifications. These behaviors use Microsoft.Extensions.Logging.
// Basic exception logging for requests & notifications
services.AddLoggingPipelineBehaviors();
The basic logging behaviors log errors when a request handler or notification handler throws. For additional performance timing (logs successful completion time and failures):
// Logs execution time and errors for requests, and errors for notifications
services.AddPerformanceLoggingPipelineBehaviors();
Choose only one of AddLoggingPipelineBehaviors or AddPerformanceLoggingPipelineBehaviors depending on whether you want timing information.
For more examples, see the SampleApp/Program.cs
file in this repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 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. |
-
net8.0
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 |
---|---|---|
0.1.0 | 78 | 8/15/2025 |