TS.MediatR 9.0.6

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

TS.MediatR

TS.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 TS.MediatR

🚀 Getting Started

using TS.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

Licensed under the MIT License.

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.6 319 6/2/2025
9.0.5 140 6/2/2025
9.0.4 136 6/2/2025
9.0.3 142 6/2/2025
9.0.2 141 6/2/2025
9.0.1 137 6/2/2025
9.0.0 139 6/2/2025