ETPackages.Mediator
2.0.0
dotnet add package ETPackages.Mediator --version 2.0.0
NuGet\Install-Package ETPackages.Mediator -Version 2.0.0
<PackageReference Include="ETPackages.Mediator" Version="2.0.0" />
<PackageVersion Include="ETPackages.Mediator" Version="2.0.0" />
<PackageReference Include="ETPackages.Mediator" />
paket add ETPackages.Mediator --version 2.0.0
#r "nuget: ETPackages.Mediator, 2.0.0"
#:package ETPackages.Mediator@2.0.0
#addin nuget:?package=ETPackages.Mediator&version=2.0.0
#tool nuget:?package=ETPackages.Mediator&version=2.0.0
📌 ETPackages.Mediator
ETPackages.Mediator is a simple and performance-oriented library for .NET. It supports IRequest, INotification, IPipelineBehavior, and works seamlessly with Dependency Injection.
📦 Features
- ✅ Request/Response (
IRequest,IRequest<TResponse>) - ✅ Notifications (
INotification) - ✅ Pipeline behaviors (
IPipelineBehavior) - ✅ Dependency Injection ready
- ✅ Fully async support
📋 Requirements
- .NET 8.0 or higher
📦 Installation
Install via NuGet:
Install-Package ETPackages.Mediator
Or via the .NET Core command line interface:
dotnet add package ETPackages.Mediator
🚀 Getting Started
using ETPackages.Mediator;
services.AddMediator(options =>
{
options.AddRegisterAssemblies(typeof(Program).Assembly);
options.AddOpenBehavior(typeof(LoggingBehavior<,>)); //with response
options.AddOpenBehavior(typeof(ValidationBehavior<,>)); //with response
options.AddOpenBehavior(typeof(ValidationBehavior<>)); //no response
});
🧩 IRequest / IRequestHandler
Define a request and its corresponding handler:
With Response
public class GetProductQuery : IRequest<ProductDto>
{
public int Id { get; set; }
}
public class GetProductQueryHandler : IRequestHandler<GetProductQuery, ProductDto>
{
public Task<ProductDto> Handle(GetProductQuery request, CancellationToken cancellationToken)
{
var product = new ProductDto { Id = request.Id, Title = "Domain-Driven Design: Tackling Complexity in the Heart of Software" };
return Task.FromResult(product);
}
}
No Response
public class CreateProductCommand : IRequest
{
public string Title { get; set; }
}
public class CreateProductCommandHandler : IRequestHandler<CreateProductCommand>
{
public Task Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
// db record
await Task.CompletedTask;
}
}
📤 Sending with IMediator
Use IMediator to send the request:
With Response
var result = await mediator.Send(new GetProductQuery { Id = 1 });
Console.WriteLine(result.Title);
No Response
await mediator.Send(new CreateProductCommand { Title = "Domain-Driven Design: Tackling Complexity in the Heart of Software" });
🔁 Pipeline Behavior
Use IPipelineBehavior for cross-cutting concerns like logging, validation, caching, etc.
With Response
public interface ILoggableRequest
{
}
public class LoggingBehavior<TRequest, TResponse>(ILogger<LoggingBehavior<TRequest, TResponse>> logger) : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>, ILoggableRequest
{
public async Task<TResponse> Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
{
var requestName = typeof(TRequest).Name;
logger.LogInformation($"Request received: {requestName}");
var response = await next();
logger.LogInformation($"Response returned: {requestName}");
return response;
}
}
No Response
public class ValidationBehavior<TRequest>(ILogger<ValidationBehavior<TRequest>> logger) : IPipelineBehavior<TRequest>
where TRequest : IRequest
{
public async Task Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
{
var requestName = typeof(TRequest).Name;
logger.LogInformation($"Request validation started: {requestName}");
await next();
logger.LogInformation($"Request validation finished: {requestName}");
}
}
📣 Notification Handling
Define a notification and its handler:
public class ProductCreatedEvent : INotification
{
public ProductCreatedEvent(int id)
{
Id = id;
}
public int Id { get; set; }
}
public class SendEmailHandler(ILogger<SendEmailHandler> logger) : INotificationHandler<ProductCreatedEvent>
{
public Task Handle(ProductCreatedEvent notification, CancellationToken cancellationToken)
{
logger.LogInformation($"Id sent: {notification.Id}");
// process send email
return Task.CompletedTask;
}
}
Publish a notification:
await mediator.Publish(new ProductCreatedEvent(product.Id));
📁 License
This project is licensed under the MIT License.
| Product | Versions 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 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 is compatible. 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. |
-
net10.0
- ETPackages.Mediator.Contracts (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection (>= 10.0.0)
-
net8.0
- ETPackages.Mediator.Contracts (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
-
net9.0
- ETPackages.Mediator.Contracts (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.