Athenex.Whimclean.Mediator 2.0.0

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

Athenex.Whimclean.Mediator

ภาษาไทย

Optional integration with Mediator (the source-generated martinothamar/Mediator, not MediatR): a pipeline behavior that commits the unit of work after each command, and a dispatcher that delivers domain events as mediator notifications. The package depends only on Mediator.Abstractions (plus Athenex.Whimclean.Application and Athenex.Whimclean.Infrastructure); the consuming application supplies the source generator.

Install

dotnet add package Athenex.Whimclean.Mediator
dotnet add package Mediator.SourceGenerator

Mediator.SourceGenerator must be referenced by the application project (or the project whose compilation contains the handlers) — the generator emits AddMediator and the dispatch code there.

Register

using Athenex.Whimclean.Mediator;

builder.Services.AddMediator(options =>
{
    options.ServiceLifetime = ServiceLifetime.Scoped;
});

builder.Services
    .AddMediatorDomainEventDispatcher() // before any default-dispatcher registration
    .AddUnitOfWorkBehavior();

// e.g. AddPostgreSqlUnitOfWork<AppDbContext>(...) afterwards; its
// TryAdd of the default dispatcher then yields to the mediator one.

AddMediatorDomainEventDispatcher registers MediatorDomainEventDispatcher as a scoped IDomainEventDispatcher with TryAdd semantics, so call it before whatever registers the default ServiceProviderDomainEventDispatcher (typically the unit-of-work registration). AddUnitOfWorkBehavior adds the open generic UnitOfWorkBehavior<,> as a scoped IPipelineBehavior<,> with TryAddEnumerable, so calling it twice registers the behavior once.

Commands commit the unit of work

UnitOfWorkBehavior<TMessage, TResponse> binds to every message implementing ICommand<TResponse> — queries and notifications are untouched. The handler mutates aggregates and returns; the behavior calls IUnitOfWork.CommitAsync afterwards, so one command spans exactly one commit and handlers never save:

using Athenex.Whimclean.Domain.Results;
using Mediator;

public sealed record PlaceOrder(Guid CustomerId) : ICommand<Result<Guid>>;

public sealed class PlaceOrderHandler(AppDbContext context)
    : ICommandHandler<PlaceOrder, Result<Guid>>
{
    public async ValueTask<Result<Guid>> Handle(
        PlaceOrder command, CancellationToken cancellationToken)
    {
        var order = Order.Place(command.CustomerId);
        context.Orders.Add(order);
        return order.Id; // no SaveChanges/Commit here
    }
}

The commit is skipped when the handler returns a failed Result (or Result<T>) or throws, leaving pending changes unpersisted.

Domain events as notifications

Domain events cannot implement INotification themselves — the domain layer takes no mediator dependency — so MediatorDomainEventDispatcher wraps each event in a DomainEventNotification and publishes it through IPublisher. Handlers derive from DomainEventHandler<TEvent>, which receives every notification and forwards only events of its TEvent type:

using Athenex.Whimclean.Application.Email;
using Athenex.Whimclean.Mediator;

public sealed class OrderPlacedHandler(IEmailSender emailSender)
    : DomainEventHandler<OrderPlaced>
{
    protected override async ValueTask HandleAsync(
        OrderPlaced domainEvent,
        CancellationToken cancellationToken)
    {
        await emailSender.SendAsync(
            new EmailMessage(
                "member@example.com",
                "Order confirmed",
                $"Order {domainEvent.OrderId} has been placed."),
            cancellationToken);
    }
}

The source generator discovers DomainEventHandler<TEvent> subclasses like any other INotificationHandler and registers them. Events are dispatched after a successful commit, exactly like the built-in pipeline (see the Athenex.Whimclean.Infrastructure README). An event with no matching handler is a no-op: the dispatcher swallows the mediator's MissingMessageHandlerException. Handler ordering and error aggregation follow the mediator's configured notification publisher; with the default publisher, handlers run sequentially and the first failure propagates.

Why the notification is not generic

The Mediator source generator does not support generic messages, and it scans every referenced assembly. Versions before 1.2.0 shipped DomainEventNotification<TEvent>, which made the generated Mediator.g.cs fail to compile (CS0246: The type or namespace name 'TEvent' could not be found) in every project that referenced this package and ran the generator. The non-generic notification plus the filtering DomainEventHandler<TEvent> base keeps handlers typed without exposing an open generic message. Each handler is invoked once per dispatched event and returns synchronously when the type does not match; the cost is a type check.

Migrating from 1.1.0: replace INotificationHandler<DomainEventNotification<TEvent>> with DomainEventHandler<TEvent>, rename Handle to HandleAsync, make it protected override, and take TEvent directly instead of notification.DomainEvent.

Repository and full architecture guide: https://github.com/athene-x/Whimclean

Product Compatible and additional computed target framework versions.
.NET 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
2.0.0 110 11/17/2026

2.0.0: first nuget.org release. Commits the unit of work after each command through a pipeline behavior and delivers domain events as the non-generic DomainEventNotification, handled by DomainEventHandler<TEvent> subclasses.