ETPackages.Mediator 2.0.0

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

📌 ETPackages.Mediator

Build & Publish NuGet Version NuGet Downloads Target Frameworks

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0 235 11/24/2025
1.0.0 359 9/18/2025