Vali-Mediator
1.1.0
See the version list below for details.
dotnet add package Vali-Mediator --version 1.1.0
NuGet\Install-Package Vali-Mediator -Version 1.1.0
<PackageReference Include="Vali-Mediator" Version="1.1.0" />
<PackageVersion Include="Vali-Mediator" Version="1.1.0" />
<PackageReference Include="Vali-Mediator" />
paket add Vali-Mediator --version 1.1.0
#r "nuget: Vali-Mediator, 1.1.0"
#:package Vali-Mediator@1.1.0
#addin nuget:?package=Vali-Mediator&version=1.1.0
#tool nuget:?package=Vali-Mediator&version=1.1.0
Vali-Mediator - Lightweight Mediator Pattern for .NET
Introduction 🚀
Welcome to Vali-Mediator, a lightweight .NET library that implements the Mediator pattern with CQRS support. Designed to simplify request handling, pipeline behaviors, and event notifications, Vali-Mediator offers a fluent and extensible API. It integrates seamlessly with dependency injection, making it perfect for managing commands, queries, notifications, and now, fire-and-forget operations and compensation flows in your .NET applications.
Installation 📦
To add Vali-Mediator to your .NET project, install it via NuGet with the following command:
dotnet add package Vali-Mediator
Ensure your project targets a compatible .NET version (e.g., .NET 7.0, 8.0, or 9.0). Vali-Mediator is lightweight and depends only on Microsoft.Extensions.DependencyInjection.Abstractions, ensuring easy integration into any .NET application.
Usage 🛠️
Vali-Mediator enables you to send requests, publish notifications, and now, dispatch fire-and-forget commands through the IValiMediator interface. It supports pipeline behaviors (e.g., validation, logging) for all dispatchable types (IDispatch), including requests, notifications, and fire-and-forget commands.
Configuration Example
Configure Vali-Mediator in your application’s startup (e.g., Program.cs in ASP.NET Core):
using Microsoft.Extensions.DependencyInjection;
using Vali_Mediator.Core.General.Extension;
var builder = WebApplication.CreateBuilder(args);
// Add Vali-Mediator services
builder.Services.AddValiMediator(config =>
{
// Register assembly containing handlers and notifications
config.RegisterServicesFromAssembly(typeof(Program).Assembly);
// Add a pipeline behavior for all dispatchable types (e.g., validation)
config.AddBehavior(typeof(IPipelineBehavior<>), typeof(ValidationBehavior<>));
// Add a pipeline behavior for all IRequest's types (e.g., validation)
config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
});
var app = builder.Build();
app.Run();
Basic Example: Sending a Request
Define a request and its handler, then send it using the mediator:
using Vali_Mediator.Core.Request;
using Vali_Mediator.Core.General.Mediator;
// Define a request
public class CreateProductCommand : IRequest<int>
{
public string Name { get; set; }
public decimal Price { get; set; }
}
// Define the handler
public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand, int>
{
public Task<int> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
// Simulate saving to a database and returning an ID
return Task.FromResult(42);
}
}
// Usage in a controller or service
public class ProductService
{
private readonly IValiMediator _mediator;
public ProductService(IValiMediator mediator)
{
_mediator = mediator;
}
public async Task<int> CreateProduct(string name, decimal price)
{
var command = new CreateProductCommand { Name = name, Price = price };
return await _mediator.Send(command);
}
}
Basic Example: Publish a Notification
Define a notification and its handler, then publish it:
using Vali_Mediator.Core.Notification;
using Vali_Mediator.Core.General.Mediator;
// Define a notification
public class ProductCreatedNotification : INotification
{
public int ProductId { get; set; }
}
// Define a handler
public class ProductCreatedLogger : INotificationHandler<ProductCreatedNotification>
{
public Task Handle(ProductCreatedNotification notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Product {notification.ProductId} created.");
return Task.CompletedTask;
}
}
// Usage
public class ProductService
{
private readonly IValiMediator _mediator;
public ProductService(IValiMediator mediator)
{
_mediator = mediator;
}
public async Task NotifyProductCreated(int productId)
{
var notification = new ProductCreatedNotification { ProductId = productId };
await _mediator.Publish(notification);
}
}
New Feature: Fire-and-Forget Commands
Vali-Mediator now supports IFireAndForget commands, allowing you to dispatch asynchronous operations without waiting for a response. This is ideal for scenarios like logging, sending emails, or any operation where you don’t need to block for a result.
Example: Sending a Fire-and-Forget Command
using Vali_Mediator.Core.FireAndForget;
using Vali_Mediator.Core.General.Mediator;
// Define a fire-and-forget command
public class LogEventCommand : IFireAndForget
{
public string EventMessage { get; set; }
}
// Define the handler
public class LogEventHandler : IFireAndForgetHandler<LogEventCommand>
{
public Task Handle(LogEventCommand command, CancellationToken cancellationToken)
{
Console.WriteLine($"Logging event: {command.EventMessage}");
return Task.CompletedTask;
}
}
// Usage
public class EventService
{
private readonly IValiMediator _mediator;
public EventService(IValiMediator mediator)
{
_mediator = mediator;
}
public async Task LogEventAsync(string message)
{
var command = new LogEventCommand { EventMessage = message };
await _mediator.Send(command);
}
}
New Feature: Compensation Flows (Saga Pattern)
Vali-Mediator now includes support for compensation flows based on the Saga pattern, allowing you to define remediation actions if a normal flow fails. This ensures data consistency in distributed systems by automatically executing compensatory actions in case of errors.
Example: Implementing Compensation
using Vali_Mediator.Core.Compensable;
using Vali_Mediator.Core.Request;
using Vali_Mediator.Core.General.Mediator;
// Define a compensable request
public class CreateOrderCommand : Compensable, IRequest<bool>
{
public string OrderId { get; set; }
public override IFireAndForget GetCompensation()
{
return new CancelOrderCommand { OrderId = OrderId };
}
}
// Define the handler with compensation
public class CreateOrderHandler : IRequestHandler<CreateOrderCommand, bool>
{
private readonly IValiMediator _mediator;
public CreateOrderHandler(IValiMediator mediator)
{
_mediator = mediator;
}
public async Task<bool> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
{
try
{
// Simulate order creation
Console.WriteLine($"Order {request.OrderId} created.");
return true;
}
catch (Exception)
{
// If something fails, compensate
await request.Compensate(_mediator, cancellationToken);
throw;
}
}
}
// Define the compensation command
public class CancelOrderCommand : IFireAndForget
{
public string OrderId { get; set; }
}
// Define the compensation handler
public class CancelOrderHandler : IFireAndForgetHandler<CancelOrderCommand>
{
public Task Handle(CancelOrderCommand command, CancellationToken cancellationToken)
{
Console.WriteLine($"Order {command.OrderId} cancelled due to failure.");
return Task.CompletedTask;
}
}
Key Methods 📝
Vali-Mediator provides two primary methods through IValiMediator:
Send (for Requests and Fire-and-Forget) 🏗️
- For requests with a response:
var command = new CreateProductCommand { Name = "Laptop", Price = 999.99m };
int productId = await mediator.Send(command);
- For fire-and-forget commands:
var logCommand = new LogEventCommand { EventMessage = "User logged in" };
await mediator.Send(logCommand);
Publish (for Notifications) 🔄
Publishes a notification to all registered handlers:
var notification = new ProductCreatedNotification { ProductId = 42 };
await mediator.Publish(notification);
Building Complex Pipelines 🧩
Vali-Mediator now supports pipeline behaviors for all dispatchable types (IDispatch), including requests, notifications, and fire-and-forget commands. This allows you to apply cross-cutting concerns like validation, logging, or authorization across different types of operations.
Example: Validation Behavior for All Dispatchable Types
using Vali_Mediator.Core.General.Behavior;
public class ValidationBehavior<TDispatch> : IPipelineBehavior<TDispatch> where TDispatch : IDispatch
{
public async Task Handle(TDispatch dispatch, Func<Task> next, CancellationToken cancellationToken)
{
// Add validation logic here (e.g., throw if invalid)
Console.WriteLine("Validating dispatch...");
await next();
}
}
// Register in configuration
services.AddValiMediator(config =>
{
config.RegisterServicesFromAssembly(typeof(Program).Assembly);
config.AddBehavior(typeof(IPipelineBehavior<>), typeof(ValidationBehavior<>));
});
Example: Validation Behavior for IRequest
using Vali_Mediator.Core.General.Behavior;
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public async Task<TResponse> Handle(TRequest request, Func<Task<TResponse>> next, CancellationToken cancellationToken) {
// Add validation logic here (e.g., throw if invalid)
Console.WriteLine("Validating dispatch...");
await next();
}
}
// Register in configuration
services.AddValiMediator(config =>
{
config.RegisterServicesFromAssembly(typeof(Program).Assembly);
config.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
});
If you need to use the implementations like INotification,IFireAndForget and IRequest you need to create the two Behavior.
public interface IPipelineBehavior<in TRequest, TResponse> where TRequest : IRequest<TResponse> for IRequest
public interface IPipelineBehavior<in TRequest> where TRequest : IDispatch for INotification and IFireAndForget
It was decided to create them separately as a matter of principle of responsibility and because the behavior is similar but nevertheless different from each other.
Comparison: Without vs. With Vali-Mediator ⚖️
Without Vali-Mediator (Manual Handling)
Manually resolving and calling handlers is verbose and tightly coupled:
var handler = serviceProvider.GetService<CreateProductCommandHandler>();
int productId = await handler.Handle(new CreateProductCommand { Name = "Laptop", Price = 999.99m }, CancellationToken.None);
With Vali-Mediator
Vali-Mediator decouples the process with a clean, unified API:
int productId = await mediator.Send(new CreateProductCommand { Name = "Laptop", Price = 999.99m });
Features and Enhancements 🌟
Recent Updates
- Fire-and-Forget Support: Added IFireAndForget for asynchronous operations without blocking for a response.
- Compensation Flows (Saga Pattern): Introduced compensation mechanisms to handle remediation for failed operations, ensuring data consistency.
- Extended Pipeline Behaviors: Pipeline behaviors now apply to all dispatchable types (IDispatch), including requests, notifications, and fire-and-forget commands.
- Multi-targeting support for .NET 7, 8, and 9.
Planned Features
- Support for request pre-processing and post-processing hooks.
- Enhanced debugging tools for pipeline execution.
Follow the project on GitHub for updates on new features and improvements!
Donations 💖
If you find Vali-Mediator useful and would like to support its development, consider making a donation:
- For Latin America: Donate via MercadoPago
- For International Donations: Donate via PayPal
Your contributions help keep this project alive and improve its development! 🚀
License 📜
This project is licensed under the Apache License 2.0.
Contributions 🤝
Feel free to open issues and submit pull requests to improve this library!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. 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 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 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. |
-
net7.0
-
net8.0
-
net9.0
NuGet packages (6)
Showing the top 5 NuGet packages that depend on Vali-Mediator:
| Package | Downloads |
|---|---|
|
Vali-Mediator.Resilience
Zero-dependency resilience library for .NET 7/8/9/10. Provides Retry (with jitter/backoff), Circuit Breaker (sliding-window state machine), Timeout (optimistic + pessimistic), Fallback, Bulkhead, Rate Limiter (partitioned per-user/IP), Hedge, and Chaos policies via a fluent builder API. Attach policies to Vali-Mediator requests via IResiliencePolicyProvider<T> (no class needed), with global fallback policy support. No Polly or other external dependencies required. |
|
|
Vali-Validation.ValiMediator
Vali-Mediator integration for Vali-Validation. Targets net7.0/net8.0/net9.0/net10.0. Adds a ValidationBehavior<TRequest, TResponse> that validates IRequest objects before they reach their handler. When TResponse is Result<T>, validation failures are returned as Result.Fail(errors, ErrorType.Validation) — no exceptions needed. For other response types, throws ValidationException. Package name: Vali-Validation.ValiMediator. Requires Vali-Mediator. |
|
|
Vali-Mediator.Observability
Zero-dependency observability library for Vali-Mediator (.NET 7/8/9/10). Provides OpenTelemetry-compatible ActivitySource tracing, pluggable metrics via IMetricsCollector, and structured request lifecycle observers via IRequestObserver. Includes pipeline behaviours for IRequest and IDispatch (INotification / IFireAndForget), console implementations for quick diagnostics, and a fluent DI registration API. No OpenTelemetry SDK or other external dependencies required. |
|
|
Vali-Mediator.AspNetCore
ASP.NET Core integration for Vali-Mediator. Maps Result<T> and Result to HTTP responses: Ok→200, Validation→400, NotFound→404, Conflict→409, Unauthorized→401, Forbidden→403, Failure→500. Provides ToActionResult() for MVC controllers and ToHttpResult() for Minimal API. |
|
|
Vali-Mediator.Caching
Caching integration library for Vali-Mediator (.NET 7/8/9/10). Provides a pluggable ICacheStore abstraction, an in-memory implementation with expiry and group-based invalidation, and two pipeline behaviors: CachingBehavior (read-through / write-through) and CacheInvalidationBehavior. Zero external dependencies beyond Microsoft.Extensions.DependencyInjection.Abstractions and Vali-Mediator. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Vali-Mediator v1.1.0 introduces significant improvements for more robust and resilient operations in distributed systems:
- Support for `IFireAndForget` commands, enabling asynchronous operations without the need to wait for an immediate response. Ideal for "fire-and-forget" scenarios such as notifications or event logging.
- Incorporation of a compensation flow based on the Saga pattern, which provides automatic remediation in case of normal flow failure. This mechanism ensures data consistency by executing compensatory actions if an error occurs, enhancing reliability in distributed transactions.
Continues to support .NET 7, 8, and 9, while maintaining features for CQRS, pipeline behaviors, and notification handling.