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
                    
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="PlainBytes.Mediation.Mediator" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PlainBytes.Mediation.Mediator" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="PlainBytes.Mediation.Mediator" />
                    
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 PlainBytes.Mediation.Mediator --version 0.1.0
                    
#r "nuget: PlainBytes.Mediation.Mediator, 0.1.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.
#:package PlainBytes.Mediation.Mediator@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PlainBytes.Mediation.Mediator&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=PlainBytes.Mediation.Mediator&version=0.1.0
                    
Install as a Cake Tool

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 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. 
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
0.1.0 78 8/15/2025