A lightweight mediator implementation designed for .NET Standard 2.0 and higher. It simplifies request handling by decoupling it from application logic, enabling easier maintenance, better testability, and clean separation of concerns — while leveraging dependency injection through Microsoft.Extensions.DependencyInjection.Abstractions.
- Target frameworks:
netstandard2.0,netstandard2.1,net8.0,net9.0,net472,net48, andnet481. - No external dependencies (except
Microsoft.Extensions.DependencyInjection.Abstractionsfor DI). - Simple setup and configuration.
- Supports multiple assemblies and namespace filtering.
- Clean, extensible, and minimalistic design.
Note: When targeting .NET Framework 4.7.2, 4.8, or 4.8.1 (net472, net48, net481), the following additional dependencies are required to support modern
async/awaitfeatures:
Microsoft.Bcl.AsyncInterfacesSystem.Runtime.CompilerServices.UnsafeSystem.Threading.Tasks.Extensions
Install via NuGet Package Manager:
Install-Package FloppyShelf.MediatorOr via .NET CLI:
dotnet add package FloppyShelf.Mediatorusing FloppyShelf.Mediator.Interfaces;
public class GetProductQuery : IRequest<Product>
{
public int Id { get; set; }
}using FloppyShelf.Mediator.Interfaces;
public class GetProductQueryHandler : IRequestHandler<GetProductQuery, Product>
{
public Task<Product> HandleAsync(GetProductQuery request, CancellationToken cancellationToken)
{
// Simulated database access
return Task.FromResult(new Product { Id = request.Id, Name = "Sample Product" });
}
}using FloppyShelf.Mediator.Extensions;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
// Register handlers from the specified assemblies
services.AddMediator(new[] { typeof(Program).Assembly });
// Optional: Pass a string[] to filter specific namespacesusing Microsoft.Extensions.DependencyInjection;
var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<IMediator>();
var result = await mediator.SendAsync(new GetProductQuery { Id = 1 });
Console.WriteLine(result.Name);IRequest<TResponse>- Represents a request expecting a response.IRequestHandler<TRequest, TResponse>- Handles a specific request.IMediator- Sends requests to handlers.
Mediator- Core implementation of the mediator pattern.ServiceCollectionExtensions- Provides theAddMediator()extension method for easy DI setup.
- Clean and Minimal - Only the essentials, no magic.
- Fully Open Source - MIT licensed.
- Great for Learning - Understand how mediators work internally.
- Extremely Lightweight - No unnecessary complexity.
