NetDevPack.Mediator
1.0.0
dotnet add package NetDevPack.Mediator --version 1.0.0
NuGet\Install-Package NetDevPack.Mediator -Version 1.0.0
<PackageReference Include="NetDevPack.Mediator" Version="1.0.0" />
<PackageVersion Include="NetDevPack.Mediator" Version="1.0.0" />
<PackageReference Include="NetDevPack.Mediator" />
paket add NetDevPack.Mediator --version 1.0.0
#r "nuget: NetDevPack.Mediator, 1.0.0"
#:package NetDevPack.Mediator@1.0.0
#addin nuget:?package=NetDevPack.Mediator&version=1.0.0
#tool nuget:?package=NetDevPack.Mediator&version=1.0.0
<img src="https://repository-images.githubusercontent.com/268701472/8bf84980-a6ce-11ea-83da-e2133c5a3a7a" alt=".NET DevPack" width="300px" />
SimpleMediator
A lightweight and straightforward mediator implementation for .NET applications, facilitating in-process messaging with minimal setup.
| Package | Version | Popularity |
|---|---|---|
NetDevPack.SimpleMediator |
Give a Star! ⭐
If you found this project helpful or it assisted you in any way, please give it a star. It helps to support the project and the developers.
Samples
You can find complete example projects demonstrating how to use the SimpleMediator in the /samples folder.
These include:
- ✅ Basic usage with
SendandPublish - ✅ Modular application structure
- ✅ Manual and automatic registration of handlers
Feel free to explore and run them to see how the mediator works in different scenarios.
Getting Started
Installation
You can install the SimpleMediator package via NuGet Package Manager or the .NET CLI:
dotnet add package NetDevPack.SimpleMediator
Using Contracts-Only Package
To reference only the contracts for SimpleMediator, which includes:
IRequest(including generic variants)- Represents a command or query that expects a single response
INotification- Represents an event broadcast to multiple handlers (if any)
Advanced Usage: Request + Notification
This example demonstrates how to combine a Request (command/query) and a Notification (event) in a real-world use case.
✅ This scenario uses only
Microsoft.Extensions.DependencyInjection.Abstractionsfor DI registration — no framework-specific packages.
1. Define the Request and Notification
public class CreateCustomerCommand : IRequest<string>
{
public string Name { get; set; }
}
public class CustomerCreatedEvent : INotification
{
public Guid CustomerId { get; }
public CustomerCreatedEvent(Guid customerId)
{
CustomerId = customerId;
}
}
2. Implement the Handlers
public class CreateCustomerHandler : IRequestHandler<CreateCustomerCommand, string>
{
private readonly IMediator _mediator;
public CreateCustomerHandler(IMediator mediator)
{
_mediator = mediator;
}
public async Task<string> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
{
var id = Guid.NewGuid();
// Simulate persistence...
// Publish event
await _mediator.Publish(new CustomerCreatedEvent(id), cancellationToken);
return $"Customer '{request.Name}' created with ID {id}";
}
}
public class SendWelcomeEmailHandler : INotificationHandler<CustomerCreatedEvent>
{
public Task Handle(CustomerCreatedEvent notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Sending welcome email to customer {notification.CustomerId}");
return Task.CompletedTask;
}
}
3. Register the Handlers (Dependency Injection)
You can register everything manually if you want full control:
services.AddSingleton<IMediator, Mediator>();
services.AddTransient<IRequestHandler<CreateCustomerCommand, string>, CreateCustomerHandler>();
services.AddTransient<INotificationHandler<CustomerCreatedEvent>, SendWelcomeEmailHandler>();
Or use assembly scanning with:
services.AddSimpleMediator(ServiceLifetime.Scoped, typeof(DependencyInjection).Assembly);
4. Execute the Flow
public class CustomerAppService
{
private readonly IMediator _mediator;
public CustomerAppService(IMediator mediator)
{
_mediator = mediator;
}
public async Task<string> CreateCustomer(string name)
{
return await _mediator.Send(new CreateCustomerCommand { Name = name });
}
}
When the CreateCustomer method is called:
CreateCustomerHandlerhandles the request- It creates and persists the customer (simulated)
- It publishes a
CustomerCreatedEvent SendWelcomeEmailHandlerhandles the event
This structure cleanly separates commands (which change state and return a result) from notifications (which communicate to the rest of the system that something happened).
Features
- Lightweight: Minimal dependencies and straightforward setup.
- In-Process Messaging: Facilitates in-process communication between components.
- Handler Registration: Automatically registers handlers from specified assemblies.
Compatibility
NetDevPack.SimpleMediator targets .NET Standard 2.1, and is compatible with .NET Core 3.1+, .NET 5+, .NET 6+, .NET 7+, .NET 8, and newer versions of the .NET runtime.
📦 Release 3.0.0 — Pipeline Behaviors Support & Breaking Changes
🚨 Breaking Changes
This release introduces PipelineBehaviors support, which required internal structural changes to the request processing pipeline. Please review your registration and handler setup to ensure compatibility.
If you're upgrading from a previous version, make sure:
- You re-register handlers and pipeline behaviors using
services.AddSimpleMediator(...)correctly. - Custom behavior logic aligns with the new
IPipelineBehavior<TRequest, TResponse>interface.
✨ New: Pipeline Behaviors Support
SimpleMediator now supports PipelineBehaviors, enabling you to intercept and extend the execution pipeline of requests and notifications — similar to what's available in MediatR.
This allows scenarios such as:
- ✅ Request validation (e.g., with FluentValidation)
- ✅ Logging and tracing
- ✅ Performance measurement
- ✅ Authorization checks
- ✅ Error handling
Example: Creating a Logging Behavior
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
public async Task<TResponse> Handle(
TRequest request,
CancellationToken cancellationToken,
RequestHandlerDelegate<TResponse> next)
{
Console.WriteLine($"[Log] Handling {typeof(TRequest).Name}");
var response = await next(cancellationToken);
Console.WriteLine($"[Log] Handled {typeof(TRequest).Name}");
return response;
}
}
Registering Behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
Multiple behaviors can be registered and are executed in the order they are registered in the container.
🧪 FluentValidation Integration Example
services.AddValidatorsFromAssemblyContaining<MyValidator>();
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
About
NetDevPack.SimpleMediatorwas originally developed by Eduardo Pires under the MIT license.
Note: This project is a fork intended to introduce specific improvements.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.1
- Microsoft.Extensions.DependencyInjection (>= 8.0.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 |
|---|---|---|
| 1.0.0 | 158 | 7/1/2026 |