TimeWarp.Mediator contracts and analyzers: IRequest, ICommand, IQuery, INotification, their handler and pipeline-behavior interfaces, ISender/IPublisher/IMediator, the membership attributes the source generator reads, and TWM001–TWM004. Reference it from message libraries without depending on a dispatcher.

Latest stable 14.0.0
Latest prerelease 15.0.0-beta.10
Downloads 23,129
Last published 2026-11-17
Target frameworks net11.0

Install

dotnet add package TimeWarp.Mediator
dotnet add package TimeWarp.Mediator --prerelease

NuGet · Source · Part of the timewarp-mediator family


TimeWarp.Mediator

Message contracts and the TWM001–TWM004, TWM012, and TWM013 analyzer for TimeWarp Mediator 15.0: requests, commands, queries, notifications, and pipeline types.

Install

dotnet add package TimeWarp.Mediator --version 15.0.0-beta.10

Without --version, that command resolves 14.0.0. Through 14.0.0 this package id was the reflection runtime (AddMediator). The 14.0.0 contracts were TimeWarp.Mediator.Contracts, and the analyzers were TimeWarp.Mediator.Analyzers. 15.0 reuses this id for the contracts and the analyzer. A host also references TimeWarp.Mediator.Generators at the same version. This package ships the TWM001–TWM004, TWM012, and TWM013 analyzer, GeneratedHandlerAttribute so a request can name a handler another generator emits, and GeneratedRequestPatternAttribute so a source type can declare requests and handlers another generator emits. There is no separate analyzers package.

Example

using Microsoft.Extensions.DependencyInjection;
using TimeWarp.Mediator;
using TimeWarp.Mediator.Generated;

ServiceCollection services = new();
services.AddGeneratedMediator();
using ServiceProvider provider = services.BuildServiceProvider();
using IServiceScope scope = provider.CreateScope();
string reply = await scope.ServiceProvider.GetRequiredService<ISender>().Send(new Ping());

public sealed class Ping : IRequest<string> { }

public sealed class PingHandler : IRequestHandler<Ping, string>
{
  public ValueTask<string> Handle(Ping request, CancellationToken cancellationToken) =>
    ValueTask.FromResult("Pong");
}

AddGeneratedMediator() is emitted into the host project by TimeWarp.Mediator.Generators, in the generated namespace (TimeWarp.Mediator.Generated unless the host sets TimeWarpMediatorNamespace). Call it from the host. Reference only this package from a project that declares messages and does not dispatch them. That project still gets TWM001–TWM004, and it opts in with [assembly: MediatorAssembly].

Requests another generator emits

Source generators do not see each other's output. When another generator emits a request and its handler into the host, declare them on a source type so Send dispatches them:

[GeneratedRequestPattern(
  "{Host}.{Contract}ActionSet.Action",
  "{Host}.{Contract}ActionSet.Handler",
  "TimeWarp.Mediator.IRequest<{ContractType}.Response>",
  typeof(CreateRole), typeof(UpdateRole))]
public partial class RoleState { }
public sealed class ContractStateAttribute(params Type[] contracts) : GeneratedRequestPatternAttribute(contracts)
{
  public const string RequestTypeName = "{Host}.{Contract}ActionSet.Action";
  public const string HandlerTypeName = "{Host}.{Contract}ActionSet.Handler";
  public const string RequestInterfaceName = "TimeWarp.Mediator.IAction";
}

The send uses the same handler call and the same woven behaviors as a request written in source. After every generator has run, TWM013 reports a declared request or handler that does not exist, a request that does not implement the declared interface, or a handler that is not its IRequestHandler.

Migration

15.0 is a breaking release. Handlers return ValueTask. The host calls AddGeneratedMediator() and supplies IServiceProvider. Registered pipeline behaviors are woven.

Steps: Migration to 15.0.

Documentation

License

Licensed as Unlicense AND Apache-2.0. The Apache-2.0 terms cover code derived from MediatR by Jimmy Bogard. The notice and unlicense files ship in this package.