AK.MediatR 9.0.0

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

AK.MediatR

AK.MediatR is a lightweight and flexible CQRS/Mediator library for .NET.

It supports IRequest, INotification, IPipelineBehavior, and works seamlessly with Dependency Injection.

🔧 Installation

Install via NuGet:

dotnet add package AK.MediatR

🚀 Getting Started

using AK.MediatR;

services.AddMediatR(options =>
{
    options.AddRegisterAssemblies(typeof(Program).Assembly);
    options.AddOpenBehavior(typeof(LoggingBehavior<,>)); // With Response
    options.AddOpenBehavior(typeof(ValidationBehavior<>)); // No Response
});

🧩 IRequest / IRequestHandler

Define a request and its corresponding handler:

With Response

public class GetUserQuery : IRequest<UserDto>
{
    public int Id { get; set; }
}

public class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto>
{
    public Task<UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        var user = new UserDto { Id = request.Id, Name = "Jhon Doe" };
        return Task.FromResult(user);
    }
}

No Response

public class CreateUserCommand : IRequest
{
    public string Name { get; set; }
}

public class GetUserQueryHandler : IRequestHandler<CreateUserCommand>
{
    public Task Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        var user = new UserDto { Name = request.Name };
        return Task.FromResult(user);
    }
}

📤 Sending with ISender

Use ISender to send the request:

With Response

var result = await sender.Send(new GetUserQuery { Id = 1 });

Console.WriteLine(result.Name);

No Response

await sender.Send(new CreateUserCommand { Name = "Jhon Doe" });

🔁 Pipeline Behavior

Use IPipelineBehavior for cross-cutting concerns like logging, validation, etc.

With Response

public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
    public async Task<TResponse> Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Request received: {typeof(TRequest).Name}");

        var response = await next();

        Console.WriteLine($"Response returned: {typeof(TResponse).Name}");

        return response;
    }
}

No Response

public class ValidationBehavior<TRequest> : IPipelineBehavior<TRequest> where TRequest : IRequest
{
    public async Task Handle(TRequest request, RequesteHandlerDelete<TResponse> next, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Request received: {typeof(TRequest).Name}");

        await next();

        Console.WriteLine($"Response returned: {typeof(TResponse).Name}");

        return response;
    }
}

📣 Notification Handling

Define a notification and its handler:

public class ProductCreatedEvent : INotification
{
    public Guid Id { get; set; }

    public ProductCreatedEvent(Guid id)
    {
        Id = id;
    }
}

public class SendEmailHandler : INotificationHandler<ProductCreatedEvent>
{
    public Task Handle(ProductCreatedEvent notification, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Id sent: {notification.Id}");
        return Task.CompletedTask;
    }
}

Publish a notification:

await sender.Publish(new ProductCreatedEvent(product.Id));

📦 Features

  • ✅ Request/Response (IRequest, IRequest<TResponse>)
  • ✅ Notifications (INotification)
  • ✅ Pipeline behaviors (IPipelineBehavior)
  • ✅ Dependency Injection ready
  • ✅ Scoped resolution and handler chaining
  • ✅ Fully async support

🔒 License

This project is MIT licensed.

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.

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
9.0.0 138 8/13/2025
8.0.1 126 8/13/2025
8.0.0 558 8/13/2025 8.0.0 is deprecated because it is no longer maintained and has critical bugs.