Brainy.Mediator 1.0.0

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

Brainy.Mediator

A lightweight, dependency-injection friendly Mediator library for .NET.

Brainy.Mediator is an open-source implementation of the Mediator pattern designed to simplify communication between different parts of your application. It helps you write clean, maintainable, and loosely coupled code without introducing unnecessary complexity.

Originally developed by Brainy Solutions for use in enterprise applications, this library has been refined and released as a free, open-source project to support the .NET community.


Why Brainy.Mediator?

As our projects grew, we wanted a mediator that was simple, lightweight, easy to understand, and easy to customize. While there are excellent mediator libraries available, we wanted complete control over the implementation and the flexibility to extend it for our own architecture.

Rather than keeping it private, we decided to share it with the community in the hope that it helps other developers build cleaner applications.


Features

  • Request / Response messaging
  • Command and Query support
  • Notification publishing
  • Multiple notification handlers
  • Pipeline Behaviors
  • Dependency Injection integration
  • Asynchronous processing
  • Generic request handlers
  • Lightweight implementation
  • No unnecessary dependencies
  • Free and Open Source

Installation

Install from NuGet:

dotnet add package Brainy.Mediator

or

Install-Package Brainy.Mediator

Registering the Mediator

builder.Services.AddScoped<IMediator, BrainyMediator>();

Register your handlers as usual.

builder.Services.AddScoped<IRequestHandler<CreateStudentCommand, long>, CreateStudentCommandHandler>();

builder.Services.AddScoped<IRequestHandler<UpdateStudentCommand, int>, UpdateStudentCommandHandler>();

Sending Requests

Define a request.

public sealed record CreateStudentCommand(
    string Name,
    string Address
) : IRequest<long>;

Create its handler.

public class CreateStudentCommandHandler
    : IRequestHandler<CreateStudentCommand, long>
{
    public async Task<long> Handle(
        CreateStudentCommand request,
        CancellationToken cancellationToken)
    {
        // business logic

        return 1;
    }
}

Send the request.

var id = await mediator.SendAsync(
    new CreateStudentCommand("Ali", "Islamabad"));

Notifications

Create a notification.

public class StudentCreatedNotification : INotification
{
    public long StudentId { get; set; }
}

Create one or more handlers.

public class EmailNotificationHandler
    : INotificationHandler<StudentCreatedNotification>
{
    public async Task HandleAsync(
        StudentCreatedNotification notification,
        CancellationToken cancellationToken)
    {
    }
}

Publish the notification.

await mediator.PublishAsync(
    new StudentCreatedNotification
    {
        StudentId = id
    });

Every registered notification handler will execute.


Pipeline Behaviors

Pipeline behaviors allow code to run before and after every request.

Typical use cases include:

  • Logging
  • Validation
  • Performance Monitoring
  • Authorization
  • Auditing
  • Caching

Example:

public class LoggingBehavior<TRequest, TResponse>
    : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    public async Task<TResponse> HandleAsync(
        TRequest request,
        Func<Task<TResponse>> next,
        CancellationToken cancellationToken)
    {
        Console.WriteLine("Before");

        var response = await next();

        Console.WriteLine("After");

        return response;
    }
}

Register it.

builder.Services.AddScoped(
    typeof(IPipelineBehavior<,>),
    typeof(LoggingBehavior<,>));

Multiple behaviors are supported and execute in the order they are registered.


Architecture

Application
      │
      ▼
IMediator
      │
      ▼
Pipeline Behaviors
      │
      ▼
Request Handler
      │
      ▼
Business Logic
      │
      ▼
Response

Why use Brainy.Mediator?

Using a mediator helps separate business logic from controllers, services, and UI layers. Instead of objects calling each other directly, requests are sent through a single mediator, making the application easier to maintain, test, and extend.

Brainy.Mediator focuses on the features most applications need while keeping the API simple and familiar.


Roadmap

Future releases may include:

  • Request Pre/Post Processors
  • Streaming Requests
  • Request Timeouts
  • Built-in Validation Pipeline
  • Performance Diagnostics
  • Source Generator Support

Contributing

Contributions, ideas, feature requests, and bug reports are always welcome.

If you'd like to improve Brainy.Mediator, feel free to open an issue or submit a pull request.


Author

Brainy.Mediator is created and maintained by Shakeel Iqbal, a Senior .NET Architect and C# Developer with extensive experience building enterprise applications and software solutions using the Microsoft technology stack.


License

This project is licensed under the MIT License.


About Brainy Solutions

We build modern software solutions using .NET, cloud technologies, AI, and enterprise architecture. As part of our commitment to the developer community, we actively open-source tools and libraries that help simplify software development.

If you find Brainy.Mediator useful, feel free to ⭐ star the repository, report issues, suggest improvements, or contribute through a pull request.

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 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 is compatible.  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 118 8/8/2026