Snowberry.Mediator 1.0.1-alpha

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

License NuGet Version

Lightweight mediator implementation (request/response, streaming requests and notifications) with support for pipeline behaviors and pluggable registration via assembly scanning or explicit type lists.

Use this package to decouple request/response handlers, streaming request handlers and notification handlers from callers, and to add cross-cutting pipeline behaviors.

Usage

Register an IMediator implementation and handlers (either by scanning assemblies or by specifying types) and call into the mediator using the IMediator surface:

  • Send a request: ValueTask<TResponse> SendAsync<TRequest, TResponse>(IRequest<TRequest, TResponse> request, CancellationToken)
  • Create a stream: IAsyncEnumerable<TResponse> CreateStreamAsync<TRequest, TResponse>(IStreamRequest<TRequest, TResponse> request, CancellationToken)
  • Publish a notification: ValueTask PublishAsync<TNotification>(TNotification notification, CancellationToken)

The mediator resolves handlers from an IServiceProvider (provided at construction) and can execute optional pipeline behavior chains when a global pipeline registry is registered.

Features

  • Core contracts and supported types (with generic signatures):
    • IRequest<TRequest, TResponse> / IRequestHandler<TRequest, TResponse>
    • IStreamRequest<TRequest, TResponse> / IStreamRequestHandler<TRequest, TResponse>
    • INotification / INotificationHandler<TNotification>
    • IPipelineBehavior<TRequest, TResponse>
    • IStreamPipelineBehavior<TRequest, TResponse>
  • Assembly scanning helper to discover handlers, pipeline behaviors and notification handlers (MediatorAssemblyHelper).
  • Global registries for pipeline and notification handlers used at runtime by Mediator.

Examples

Below are minimal examples demonstrating common usage patterns.

Microsoft Dependency Injection

The repository contains an integration extension (Snowberry.Mediator.Extensions.DependencyInjection) which exposes AddSnowberryMediator to register the mediator and handlers into an IServiceCollection.

Example: register by scanning the current assembly and enable pipeline/notification scanning:

using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

services.AddSnowberryMediator(options =>
{
    options.Assemblies = new List<Assembly> { Assembly.GetExecutingAssembly() };
    options.ScanNotificationHandlers = true;
    options.ScanPipelineBehaviors = true;
});

var provider = services.BuildServiceProvider();
var mediator = provider.GetRequiredService<Snowberry.Mediator.Abstractions.IMediator>();

// Send a request (example)
// await mediator.SendAsync(new MyRequest(...));

Example: explicit registration of pipeline behaviors (ordered) and notification handlers (no scanning):

services.AddSnowberryMediator(opts =>
{
    opts.Assemblies = new List<Assembly>();
    opts.RegisterPipelineBehaviors = true;
    opts.PipelineBehaviorTypes = new List<Type>
    {
        typeof(MyApp.Pipeline.LoggingBehavior<,>),
        typeof(MyApp.Pipeline.ValidationBehavior<,>)
    };

    opts.RegisterNotificationHandlers = true;
    opts.NotificationHandlerTypes = new List<Type>
    {
        typeof(MyApp.Notifications.SomeNotificationHandler)
    };
});

Pipeline behavior ordering and priority

Pipeline behaviors are executed in a linked delegate chain. The order in which behaviors are executed matters because each behavior receives a NextPipeline delegate that it must call to continue the chain.

Ordering rules:

  • When you explicitly provide PipelineBehaviorTypes (or StreamPipelineBehaviorTypes), the order of types in the list is preserved and used as the base ordering.
  • When pipeline behaviors are discovered via scanning, or when combining scanned and explicit lists, the framework can use the PipelineOverwritePriorityAttribute applied to behavior types to determine priority.
  • PipelineOverwritePriorityAttribute contains an integer Priority property. Higher values indicate higher priority — behaviors with a higher Priority value are executed earlier in the pipeline chain.

Practical guidance:

  • If you need a behavior to run first (for example, logging or diagnostics that should wrap everything), give it a higher Priority.
  • If you mix scanned behaviors and an explicit list, set priorities on scanned implementations when their relative position matters, or prefer explicit ordering for predictable placement.
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 (3)

Showing the top 3 NuGet packages that depend on Snowberry.Mediator:

Package Downloads
Snowberry.Mediator.Extensions.DependencyInjection

Extension for the main package to work with `Microsoft.Extensions.DependencyInjection`.

Snowberry.Mediator.DependencyInjection.Shared

A small helper library for supporting multiple Dependency Injection frameworks.

Snowberry.Mediator.DependencyInjection

Extension for the main package to work with `Snowberry.DependencyInjection`.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1-alpha 29 9/23/2025
1.0.0-alpha 122 9/9/2025