NocturnalGroup.SimpleMediator.InMemory 1.0.0-preview.1

This is a prerelease version of NocturnalGroup.SimpleMediator.InMemory.
dotnet add package NocturnalGroup.SimpleMediator.InMemory --version 1.0.0-preview.1                
NuGet\Install-Package NocturnalGroup.SimpleMediator.InMemory -Version 1.0.0-preview.1                
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="NocturnalGroup.SimpleMediator.InMemory" Version="1.0.0-preview.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NocturnalGroup.SimpleMediator.InMemory --version 1.0.0-preview.1                
#r "nuget: NocturnalGroup.SimpleMediator.InMemory, 1.0.0-preview.1"                
#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.
// Install NocturnalGroup.SimpleMediator.InMemory as a Cake Addin
#addin nuget:?package=NocturnalGroup.SimpleMediator.InMemory&version=1.0.0-preview.1&prerelease

// Install NocturnalGroup.SimpleMediator.InMemory as a Cake Tool
#tool nuget:?package=NocturnalGroup.SimpleMediator.InMemory&version=1.0.0-preview.1&prerelease                

<img align="right" width="256" height="256" src="Assets/Logo.png">

<div id="user-content-toc"> <ul align="center" style="list-style: none;"> <summary> <h1>SimpleMediator</h1> </summary> </ul> </div>

A dead simple mediator implementation

About ยท Getting Started ยท License ยท Contributing


SimpleMediator is an implementation of the mediator pattern. Out the box it supports events and requests, with an in-memory implementation.

Installing

How you install SimpleMediator depends on the needs of your projects.

Every project that interacts with SimpleMediator should install the abstractions via NuGet:

dotnet add package NocturnalGroup.SimpleMediator.Abstractions

Optionally, install the source generator via NuGet:

dotnet add package NocturnalGroup.SimpleMediator.SourceGenerator

Then in your host project, either implement the interfaces or install the In-Memory implementations via NuGet:

dotnet add package NocturnalGroup.SimpleMediator.InMemory

Quickstart

For a detailed walkthrough of SimpleMediator, check out our tutorial.

Events

var services = new ServiceCollection();

// ๐Ÿ‘‡ Add the in-memory mediator implementation.
services.AddInMemoryMediator();

// ๐Ÿ‘‡ Add the event handler.
services.AddEventHandler<UserCreatedEvent, UserCreatedHandler>(); // Transient lifetime
services.AddEventHandler<UserCreatedEvent, UserCreatedHandler>(ServiceLifetime.Scoped); // Custom lifetime

// ๐Ÿ‘‡ Create an event type.
public sealed record UserCreatedEvent(string Username) : IEvent;

// ๐Ÿ‘‡ Create an event handler.
public sealed class UserCreatedHandler : IEventHandler<UserCreatedEvent>
{
  public async Task HandleEventAsync(UserCreatedEvent data, CancellationToken _)
  {
    await Console.Out.WriteLineAsync($"User {data.Username} was created");
  }
}

// ๐Ÿ‘‡ Publish an event.
public sealed class UserService(IEventSender sender)
{
  public async Task CreateUserAsync(string username)
  {
    // ... User Creation Logic ...

    await sender.PublishEventAsync(new UserCreatedEvent(username));
  }
}

Requests

var services = new ServiceCollection();

// ๐Ÿ‘‡ Add the in-memory mediator implementation.
services.AddInMemoryMediator();

// ๐Ÿ‘‡ Add the request handler.
services.AddRequestHandler<GetUserRequest, GetUserResponse, GetUserHandler>(); // Transient lifetime
services.AddRequestHandler<GetUserRequest, GetUserResponse, GetUserHandler>(ServiceLifetime.Scoped); // Custom lifetime

// ๐Ÿ‘‡ Create a request type.
public sealed record GetUserRequest(Guid Id) : IRequest<GetUserResponse>;

// ๐Ÿ‘‡ Create a response type.
public sealed record GetUserResponse(User User);

// ๐Ÿ‘‡ Create a request handler.
public sealed class GetUserHandler : IRequestHandler<GetUserRequest, GetUserResponse>
{
  public async Task<GetUserResponse> HandleRequestAsync(GetUserRequest request, CancellationToken _)
  {
    // ... User Query Logic ...

    return new GetUserResponse(user);
  }
}

// ๐Ÿ‘‡ Send a request.
public sealed class RequestSender(IRequestSender sender)
{
  public async Task HandleClickAsync(Guid userId)
  {
    // Without Source Generator
    var request = new GetUserRequest(userId);
    var response = await sender.SendRequestAsync<GetUserRequest, GetUserResponse>(request);
    await Console.Out.WriteLineAsync(response.User.Username);

    // With Source Generator
    var request = new GetUserRequest(userId);
    var response = await sender.SendRequestAsync(request);
    await Console.Out.WriteLineAsync(response.User.Username);
  }
}

Versioning

We use Semantic Versioning to clearly communicate changes:

  • Major version changes indicate breaking updates
  • Minor version changes add features in a backward-compatible way
  • Patch version changes include backward-compatible bug fixes
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. 
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-preview.1 36 2/19/2025