SharedKernel.MediatoR.Abstractions 1.1.0

dotnet add package SharedKernel.MediatoR.Abstractions --version 1.1.0
                    
NuGet\Install-Package SharedKernel.MediatoR.Abstractions -Version 1.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="SharedKernel.MediatoR.Abstractions" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharedKernel.MediatoR.Abstractions" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SharedKernel.MediatoR.Abstractions" />
                    
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 SharedKernel.MediatoR.Abstractions --version 1.1.0
                    
#r "nuget: SharedKernel.MediatoR.Abstractions, 1.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 SharedKernel.MediatoR.Abstractions@1.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=SharedKernel.MediatoR.Abstractions&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SharedKernel.MediatoR.Abstractions&version=1.1.0
                    
Install as a Cake Tool

MediatoR

NuGet License: CC BY 4.0 .NET

A lightweight mediator implementation for .NET with middleware support, enabling clean CQRS and messaging patterns in your applications.

Features

Simple & Lightweight - Minimal dependencies and clean API 🚀 Async/Await Support - Fully asynchronous request/response handling 🔌 Middleware Pipeline - Extensible middleware support for cross-cutting concerns 📢 Notification System - Publish/subscribe pattern for notifications 🎯 CQRS Ready - Perfect for Command Query Responsibility Segregation 🛠️ Dependency Injection - Built-in support for .NET DI container 📦 Zero Configuration - Works out of the box with minimal setup

Installation

Install MediatoR via NuGet Package Manager: dotnet add package MediatoR

Or via Package Manager Console: Install-Package MediatoR

Quick Start

1. Create a Request and Handler

using MediatoR.Abstractions; // Define a request public record GetUserQuery(int UserId) : IRequest<User>; // Implement the handler public class GetUserHandler : IRequestHandler<GetUserQuery, User> { public async Task<User> Handle(GetUserQuery request, CancellationToken cancellationToken) { // Your business logic here return await GetUserFromDatabase(request.UserId); } }

2. Setup the Mediator

using MediatoR; using Microsoft.Extensions.DependencyInjection; var services = new ServiceCollection(); var mediator = new MediatoR.MediatoR(services.BuildServiceProvider()); // Register your handlers mediator.RegisterHandler(new GetUserHandler());

3. Send Requests

// Send a request and get response var user = await mediator.Send<GetUserQuery, User>(new GetUserQuery(123)); Console.WriteLine($"User: {user.Name}"); // Send a command without response await mediator.Send(new CreateUserCommand("John Doe"));

Core Concepts

Requests with Response

// Query with response public record GetProductQuery(int Id) : IRequest<Product>; public class GetProductHandler : IRequestHandler<GetProductQuery, Product> { public async Task<Product> Handle(GetProductQuery request, CancellationToken cancellationToken) { // Implementation return new Product { Id = request.Id, Name = "Sample Product" }; } }

Commands without Response

// Command without response public record DeleteProductCommand(int Id) : IRequest; public class DeleteProductHandler : IRequestHandler<DeleteProductCommand> { public async Task Handle(DeleteProductCommand request, CancellationToken cancellationToken) { // Implementation await DeleteProductFromDatabase(request.Id); } }

Notifications

// Define a notification public record UserCreatedNotification(int UserId, string Email) : INotification; // Handle notifications public class UserCreatedHandler : INotificationHandler<UserCreatedNotification> { public async Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken) { // Send welcome email, update statistics, etc. await SendWelcomeEmail(notification.Email); } } // Publish notifications await mediator.Publish(new UserCreatedNotification(123, "user@example.com"));

Middleware Support

MediatoR supports middleware for cross-cutting concerns like logging, validation, caching, etc. public class LoggingMiddleware : IMediatorMiddleware { public async Task InvokeAsync(object request, Func<object, Task> next) { Console.WriteLine($"Executing: {request.GetType().Name}"); var stopwatch = Stopwatch.StartNew(); await next(request); stopwatch.Stop();

Console.WriteLine($"Completed: {request.GetType().Name} in {stopwatch.ElapsedMilliseconds}ms");

}

// Register middleware mediator.RegisterMiddleware(new LoggingMiddleware()); mediator.RegisterMiddleware(new ValidationMiddleware());

Middleware executes in the order of registration, creating a pipeline around your handlers.

Dependency Injection Integration

MediatoR automatically resolves handlers from the DI container: // In your Startup.cs or Program.cs services.AddScoped<IRequestHandler<GetUserQuery, User>, GetUserHandler>(); services.AddScoped<INotificationHandler<UserCreatedNotification>, UserCreatedHandler>(); var mediator = new MediatoR.MediatoR(serviceProvider); // No need to manually register - will be resolved automatically var user = await mediator.Send<GetUserQuery, User>(new GetUserQuery(123));

Advanced Usage

Generic Response Wrapper

public class Response<T> { public T Data { get; set; } public bool Success { get; set; } public string Message { get; set; } } public record CreateProductCommand(string Name, decimal Price) : IRequest<Response<Product>>; public class CreateProductHandler : IRequestHandler<CreateProductCommand, Response<Product>> { public async Task<Response<Product>> Handle(CreateProductCommand request, CancellationToken cancellationToken) { var product = new Product { Name = request.Name, Price = request.Price }; await SaveToDatabase(product);

public class Response<T> { public T Data { get; set; } public bool Success { get; set; } public string Message { get; set; } } public record CreateProductCommand(string Name, decimal Price) : IRequest<Response<Product>>; public class CreateProductHandler : IRequestHandler<CreateProductCommand, Response<Product>> { public async Task<Response<Product>> Handle(CreateProductCommand request, CancellationToken cancellationToken) { var product = new Product { Name = request.Name, Price = request.Price }; await SaveToDatabase(product); return new Response<Product> { Data = product, Success = true, Message = "Product created successfully" }; } }

Error Handling Middleware

public class ExceptionHandlingMiddleware : IMediatorMiddleware { public async Task InvokeAsync(object request, Func<object, Task> next) { try { await next(request); } catch (Exception ex) { // Log exception Console.WriteLine($"Error processing {request.GetType().Name}: {ex.Message}"); throw; // Re-throw or handle as needed } } }

Best Practices

  1. Keep Handlers Focused - One handler per request/command
  2. Use Records - Immutable request objects are preferred
  3. Middleware Order Matters - Register middleware in the correct execution order
  4. Async All the Way - Use async/await throughout your pipeline
  5. Separate Concerns - Use different request types for queries vs commands

Requirements

  • .NET 9.0 or later
  • Microsoft.Extensions.DependencyInjection.Abstractions 9.0.7+

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Creative Commons Attribution 4.0 License (CC BY 4.0).

Author

Jorge Blanchard Cruz

Support

If you find this project useful, consider supporting the development:

  • ⭐ Star this repository
  • 🐛 Report issues
  • 💝 Contact for donations: jorgeblanchardcruz@outlook.com

MediatoR - Simple, powerful, and extensible mediator pattern implementation for .NET

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SharedKernel.MediatoR.Abstractions:

Package Downloads
SharedKernel.MediatoR

MediatoR - A lightweight mediator implementation for .NET with middleware support

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 183 8/4/2025
1.0.0 179 8/4/2025