LevapTech.SimpleMediator 1.0.0

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

SimpleMediator

A lightweight, dependency-injected mediator library for .NET, supporting CQRS, Pub-Sub, and pipeline behaviors. SimpleMediator helps you decouple your application logic by handling commands, queries, and events through a clean, extensible interface.

Features

  • CQRS (Command Query Responsibility Segregation): Separate command and query handling for clear, maintainable code.
  • Pub-Sub (Event Publishing): Publish events to multiple handlers for robust event-driven architectures.
  • Dependency Injection: Built-in support for Microsoft.Extensions.DependencyInjection.
  • Pipeline Behaviors: Add cross-cutting concerns (logging, validation, etc.) via pipeline behaviors.

Getting Started

1. Register Services

Add SimpleMediator and your handlers to the DI container:

using LevapTech.SimpleMediator;
using LevapTech.SimpleMediator.Abstractions;

var services = new ServiceCollection();

// Register mediator
services.AddSingleton<ISimpleMediator, SimpleMediator>();

// Register handlers
services.AddTransient<ICommandHandler<CreateOrderCommand, Guid>, CreateOrderCommandHandler>();
services.AddTransient<IQueryHandler<GetOrderByIdQuery, Order>, GetOrderByIdQueryHandler>();
services.AddTransient<IEventHandler<OrderCreatedEvent>, OrderCreatedEventHandler>();

// Register pipeline behaviors (optional)
services.AddTransient<IPipelineBehavior<CreateOrderCommand, Guid>, LoggingBehavior<CreateOrderCommand, Guid>>();


2. Define Commands, Queries, and Events

// Command
public record CreateOrderCommand(string Product, int Quantity) : ICommand<Guid>;

// Query
public record GetOrderByIdQuery(Guid OrderId) : IQuery<Order>;

// Event
public record OrderCreatedEvent(Guid OrderId) : IEvent;


3. Implement Handlers

// Command Handler
public class CreateOrderCommandHandler : ICommandHandler<CreateOrderCommand, Guid>
{
    public Task<Guid> HandleAsync(CreateOrderCommand command, CancellationToken cancellationToken)
    {
        // ... create order logic ...
        return Task.FromResult(Guid.NewGuid());
    }
}

// Query Handler
public class GetOrderByIdQueryHandler : IQueryHandler<GetOrderByIdQuery, Order>
{
    public Task<Order> HandleAsync(GetOrderByIdQuery query, CancellationToken cancellationToken)
    {
        // ... fetch order logic ...
        return Task.FromResult(new Order());
    }
}

// Event Handler
public class OrderCreatedEventHandler : IEventHandler<OrderCreatedEvent>
{
    public Task HandleAsync(OrderCreatedEvent @event, CancellationToken cancellationToken)
    {
        // ... handle event ...
        return Task.CompletedTask;
    }
}


4. Use the Mediator

public class OrdersController : ControllerBase
{
    private readonly ISimpleMediator _mediator;

    public OrdersController(ISimpleMediator mediator)
    {
        _mediator = mediator;
    }

    [HttpPost]
    public async Task<IActionResult> CreateOrder(CreateOrderCommand command)
    {
        var orderId = await _mediator.SendAsync<CreateOrderCommand, Guid>(command);
        await _mediator.PublishAsync(new OrderCreatedEvent(orderId));
        return Ok(orderId);
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetOrder(Guid id)
    {
        var order = await _mediator.QueryAsync<GetOrderByIdQuery, Order>(new GetOrderByIdQuery(id));
        return Ok(order);
    }
}


5. Add Pipeline Behaviors (Optional)

Pipeline behaviors allow you to add cross-cutting logic (e.g., logging, validation):

public class LoggingBehavior<TRequest, TResult> : IPipelineBehavior<TRequest, TResult>
{
    public async Task<TResult> HandleAsync(TRequest request, CancellationToken cancellationToken, Func<Task<TResult>> next)
    {
        Console.WriteLine($"Handling {typeof(TRequest).Name}");
        var result = await next();
        Console.WriteLine($"Handled {typeof(TRequest).Name}");
        return result;
    }
}

Register your behavior in DI as shown above.


License

MIT

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.

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.0 198 5/26/2025