CustomMediator 1.0.3

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

Mediator

NuGet License: MIT .NET

A lightweight, fast mediator pattern implementation for .NET that serves as a zero-dependency alternative to MediatR. Perfect for projects requiring minimal overhead and straightforward request/response messaging with dependency injection.

Mediator Pattern: Encapsulates how a set of objects interact without them being tightly coupled. Routes requests to appropriate handlers through a central mediator component.

Why Choose This Mediator?

Feature This Package MediatR
Core Mediator Yes Yes
Request/Response Yes Yes
DI Integration Yes Yes
CancellationToken Support Yes Yes
Package Size Small (~30 KB) Larger (~500 KB)
Zero Dependencies Yes No
Learning Curve Very Easy Moderate
Pipelines/Behaviors No Yes
Notifications No Yes

Choose this Mediator if you:

  • Need a simple, focused request/response pattern
  • Want minimal dependencies and fast startup
  • Prefer inspectable, straightforward code
  • Don't need advanced features like pipelines or pub/sub

Choose MediatR if you:

  • Need pipeline behaviors and cross-cutting concerns
  • Require pub/sub notifications
  • Are building enterprise-scale messaging infrastructure

Features

  • Lightweight � No external dependencies
  • High Performance � Minimal dispatch overhead
  • Fully Async � Native async/await with Task<T>
  • CancellationToken Support � Full cooperative cancellation
  • DI-First � Works with Microsoft.Extensions.DependencyInjection
  • Clean Exceptions � Handler exceptions propagate naturally
  • .NET 8 Native � Built for modern .NET with C# 12 support
  • Testable � Simple interface-based design for easy mocking

Installation

NuGet Package Manager:

dotnet add package CustomMediator

Package Manager Console:

Install-Package CustomMediator

.NET CLI:

dotnet package add CustomMediator

Quick Start

  1. Define your request and response types:
using Mediator;

public class GetProductRequest : IRequest<ProductResponse>
{
    public int ProductId { get; set; }
}

public class ProductResponse
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}
  1. Create a handler:
using Mediator;
using System.Threading;
using System.Threading.Tasks;

public class GetProductHandler : IRequestHandler<GetProductRequest, ProductResponse>
{
    private readonly IProductService _productService;

    public GetProductHandler(IProductService productService)
    {
        _productService = productService;
    }

    public async Task<ProductResponse> Handle(GetProductRequest request, CancellationToken cancellationToken)
    {
        var product = await _productService.GetProductAsync(request.ProductId, cancellationToken);

        return new ProductResponse
        {
            Id = product.Id,
            Name = product.Name,
            Price = product.Price
        };
    }
}
  1. Register services and handlers in DI:
using Microsoft.Extensions.DependencyInjection;
using Mediator;
using Mediator.Interfaces;

var services = new ServiceCollection();

services.AddScoped<IMediator>(sp => new Mediator(sp));
services.AddScoped<IRequestHandler<GetProductRequest, ProductResponse>, GetProductHandler>();
services.AddScoped<IProductService, ProductService>();

var provider = services.BuildServiceProvider();
  1. Send a request:
var mediator = provider.GetRequiredService<IMediator>();
var response = await mediator.Send<ProductResponse>(new GetProductRequest { ProductId = 42 });
Console.WriteLine($"Product: {response.Name} - ${response.Price}");

Core API Reference

Interfaces:

public interface IRequest<TResponse> { }

public interface IRequestHandler<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
}

public interface IMediator
{
    Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
}

Examples

  1. Simple greeting:
public class GreetRequest : IRequest<string> { public string Name { get; set; } }
public class GreetHandler : IRequestHandler<GreetRequest, string>
{
    public Task<string> Handle(GreetRequest request, CancellationToken cancellationToken)
    {
        return Task.FromResult($"Hello, {request.Name}!");
    }
}
  1. Long-running operation with cancellation:
public class LongRunningRequest : IRequest<string> { public int Seconds { get; set; } }
public class LongRunningHandler : IRequestHandler<LongRunningRequest, string>
{
    public async Task<string> Handle(LongRunningRequest request, CancellationToken cancellationToken)
    {
        await Task.Delay(TimeSpan.FromSeconds(request.Seconds), cancellationToken);
        return "Done";
    }
}
  1. Exception propagation:
public class FailingRequest : IRequest<string> { }
public class FailingHandler : IRequestHandler<FailingRequest, string>
{
    public Task<string> Handle(FailingRequest request, CancellationToken cancellationToken)
    {
        throw new InvalidOperationException("Handler failed");
    }
}

// The mediator will propagate the InvalidOperationException to the caller.

Registration Patterns

Register handlers individually or use a helper to scan assemblies and register all IRequestHandler implementations.

Best Practices

  • Keep handlers focused and single-responsibility.
  • Respect CancellationToken in handlers.
  • Throw specific exceptions for error cases.
  • Use logging for important operations.
  • Validate inputs early.

Unit Testing

Handlers are easy to unit test by mocking dependencies and invoking Handle directly.

Troubleshooting

  • "No handler found" � ensure the handler is registered in DI.
  • "TaskCanceledException" � ensure cancellation token timeouts are configured and handled.
  • Null dependencies � register dependencies before registering handlers.

Resources

License

This project is licensed under the MIT License. See LICENSE for details.

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 was computed.  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.
  • net8.0

    • No dependencies.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 0 5/4/2026