PlopyBlopy.MediatoR.Alternative.Lite 1.0.20

There is a newer version of this package available.
See the version list below for details.
dotnet add package PlopyBlopy.MediatoR.Alternative.Lite --version 1.0.20
                    
NuGet\Install-Package PlopyBlopy.MediatoR.Alternative.Lite -Version 1.0.20
                    
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="PlopyBlopy.MediatoR.Alternative.Lite" Version="1.0.20" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PlopyBlopy.MediatoR.Alternative.Lite" Version="1.0.20" />
                    
Directory.Packages.props
<PackageReference Include="PlopyBlopy.MediatoR.Alternative.Lite" />
                    
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 PlopyBlopy.MediatoR.Alternative.Lite --version 1.0.20
                    
#r "nuget: PlopyBlopy.MediatoR.Alternative.Lite, 1.0.20"
                    
#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.
#addin nuget:?package=PlopyBlopy.MediatoR.Alternative.Lite&version=1.0.20
                    
Install as a Cake Addin
#tool nuget:?package=PlopyBlopy.MediatoR.Alternative.Lite&version=1.0.20
                    
Install as a Cake Tool

MediatoR.Alternative.Lite

A simplified alternative to MediatR with core features for request handling and pipeline behavior support.

Features

  • 🚀 Minimalistic mediator implementation (Sender + Pipeline)
  • 🔗 Middleware-like behavior support (validation, logging, etc.)
  • ✅ FluentResults integration for error handling
  • 💉 Auto-registration of handlers via DI (Microsoft.Extensions.DependencyInjection)
  • 🛠️ Extensible architecture

Quick Start

1. Register Services

services
    .AddMediatorAlt() // Auto-registers all IRequestHandlers in the current assembly
    // Optional behaviors (registration order = execution order):
    .AddMediatorAltLogging()     // Logging (outer layer)
    .AddMediatorAltFluentValidation(); // Validation (inner layer)

2. Create Request

public record GetUserQuery(int UserId) : IRequest<User>;

3. Implement Handler

public class GetUserHandler : IRequestHandler<GetUserQuery, User>
{
    public async Task<Result<User>> Handle(GetUserQuery request, CancellationToken ct)
    {
        // Return result via FluentResults
        return Result.Ok(new User(...));
    }
}

4. Usage

public class MyService
{
    private readonly ISender _sender;

    public MyService(ISender sender) => _sender = sender;

    public async Task Execute()
    {
        var result = await _sender.Send(new GetUserQuery(123));

        if (result.IsSuccess)
        {
            var user = result.Value;
        }
        else
        {
            var errors = result.Errors; // Access error list
            // Handle errors
        }
    }
}

Pipeline Behaviors

Validation

  • Calling AddMediatorAltFluentValidation() auto-registers all validators implementing IValidator<TRequest>

Register validators (FluentValidation):

public class GetUserValidator : AbstractValidator<GetUserQuery>
{
    public GetUserValidator()
    {
        RuleFor(x => x.UserId).GreaterThan(0);
    }
}

Custom Behavior

public class TimingBehavior<TRequest, TResponse> 
    : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    public async Task<Result<TResponse>> Handle(
        TRequest request,
        CancellationToken ct,
        Func<Task<Result<TResponse>>> next)
    {
        var sw = Stopwatch.StartNew();
        var response = await next();
        sw.Stop();
        
        Console.WriteLine($"Execution time: {sw.ElapsedMilliseconds}ms");
        return response;
    }
}

// Registration with order specification:
services.AddTransient(
	typeof(IPipelineBehavior<,>), typeof(TimingBehavior<,>)); // Executes after other registered behaviors

Implementation Details

Execution Order

Behaviors execute in registration order:

[Behavior 1] -> [Behavior 2] -> ... -> [Handler]

Example for .AddLogging().AddValidation():

[Logging] -> [Validation] -> [Handler]

Key Implementation Notes

  • Handlers (IRequestHandler<,>) are registered as Scoped
  • Behaviors (IPipelineBehavior<,>) execute in registration order
  • All errors are aggregated via FluentResults.Result

Best Practices

  1. One request - one handler
  2. Use Result<T> (FluentResults) for data returns
  3. Separate concerns:
    • Validation → in ValidationBehavior
    • Business rules → in handlers
    • Cross-cutting concerns → in PipelineBehaviors
  4. Register behaviors in required order
  5. Handle errors via result.Errors
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
1.0.21 254 6/9/2025
1.0.20 129 6/2/2025