LightMediator 1.0.0

LightMediator

A lightweight, free alternative to MediatR for implementing the mediator pattern in .NET applications.

Features

  • Lightweight: Minimal dependencies and overhead
  • Free & Open Source: MIT licensed, no paid tiers
  • Simple API: Easy to use, similar to MediatR
  • Type-Safe: Full TypeScript-style type safety
  • Performance: Efficient handler resolution with caching
  • Dependency Injection: Built-in support for Microsoft.Extensions.DependencyInjection

Installation

dotnet add package LightMediator

Or via Package Manager:

Install-Package LightMediator

Quick Start

1. Register LightMediator

using LightMediator;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

// Register LightMediator
services.AddLightMediator();

// Register your handlers
services.AddScoped<IRequestHandler<GetUserQuery, UserDto>, GetUserQueryHandler>();
services.AddScoped<IRequestHandler<CreateUserCommand>, CreateUserCommandHandler>();

var serviceProvider = services.BuildServiceProvider();
var mediator = serviceProvider.GetRequiredService<IMediator>();

2. Define Your Requests

// Request with response
public class GetUserQuery : IRequest<UserDto>
{
    public int UserId { get; set; }
}

// Request without response
public class CreateUserCommand : IRequest
{
    public string Name { get; set; }
    public string Email { get; set; }
}

3. Create Handlers

public class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto>
{
    public Task<UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken = default)
    {
        // Your logic here
        return Task.FromResult(new UserDto
        {
            Id = request.UserId,
            Name = "John Doe"
        });
    }
}

public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand>
{
    public Task Handle(CreateUserCommand request, CancellationToken cancellationToken = default)
    {
        // Your logic here
        return Task.CompletedTask;
    }
}

4. Use the Mediator

// Send request with response
var query = new GetUserQuery { UserId = 123 };
var user = await mediator.Send(query);

// Send request without response
var command = new CreateUserCommand { Name = "Jane", Email = "jane@example.com" };
await mediator.Send(command);

Automatic Handler Registration

You can automatically register all handlers in an assembly:

services.AddLightMediator(typeof(GetUserQueryHandler).Assembly);

This will scan the specified assembly and register all classes that implement IRequestHandler<,> or IRequestHandler<>.

Advanced Usage

Cancellation Tokens

All handlers support cancellation tokens:

var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(5));

var result = await mediator.Send(query, cts.Token);

Error Handling

try
{
    var result = await mediator.Send(query);
}
catch (InvalidOperationException ex)
{
    // Handler not found or not registered
    Console.WriteLine(ex.Message);
}
catch (OperationCanceledException)
{
    // Operation was cancelled
}

Comparison with MediatR

Feature LightMediator MediatR
License MIT (Free) Commercial (Paid)
Size Lightweight Larger
Dependencies Minimal More dependencies
Performance Fast Fast
API Compatibility Similar Original

Requirements

  • .NET 9.0 or later
  • Microsoft.Extensions.DependencyInjection

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Why LightMediator?

MediatR transitioned from a free, open-source library to a commercial product. LightMediator provides a free, lightweight alternative that maintains a similar API while being completely open source and free to use.

No packages depend on LightMediator.

Version Downloads Last updated
1.0.0 2 12/13/2025
0.5.2 1 12/13/2025
0.5.1.4 1 12/13/2025
0.5.1.3 1 12/13/2025
0.5.1.2 1 12/13/2025
0.5.1.1 1 12/13/2025
0.5.1 1 12/13/2025
0.5.0 1 12/13/2025
0.4.8 1 12/13/2025
0.4.7 1 12/13/2025
0.4.6 1 12/13/2025
0.4.5 1 12/13/2025
0.4.4 1 12/13/2025
0.4.3 1 12/13/2025
0.4.2 1 12/13/2025
0.4.1 1 12/13/2025
0.4.0 1 12/13/2025
0.3.0 1 12/13/2025
0.2.0 1 12/13/2025
0.1.0 1 12/13/2025