Foundatio.Mediator
1.0.0-preview9
Prefix Reserved
This is a prerelease version of Foundatio.Mediator.
dotnet add package Foundatio.Mediator --version 1.0.0-preview9
NuGet\Install-Package Foundatio.Mediator -Version 1.0.0-preview9
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="Foundatio.Mediator" Version="1.0.0-preview9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Foundatio.Mediator" Version="1.0.0-preview9" />
<PackageReference Include="Foundatio.Mediator" />
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 Foundatio.Mediator --version 1.0.0-preview9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Foundatio.Mediator, 1.0.0-preview9"
#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 Foundatio.Mediator@1.0.0-preview9
#: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=Foundatio.Mediator&version=1.0.0-preview9&prerelease
#tool nuget:?package=Foundatio.Mediator&version=1.0.0-preview9&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Blazingly fast, convention-based C# mediator powered by source generators and interceptors.
โจ Why Choose Foundatio Mediator?
- ๐ Near-direct call performance - Zero runtime reflection, minimal overhead
- โก Convention-based - No interfaces or base classes required
- ๐ง Full DI support - Microsoft.Extensions.DependencyInjection integration
- ๐งฉ Plain handler classes - Drop in static or instance methods anywhere
- ๐ช Middleware pipeline - Before/After/Finally hooks with state passing
- ๐ฏ Built-in Result<T> - Rich status handling without exceptions
- ๐ Tuple returns - Automatic cascading messages
- ๐ Compile-time safety - Early validation and diagnostics
- ๐งช Easy testing - Plain objects, no framework coupling
- ๐ Superior debugging - Short, simple call stacks
๐ Complete Example
1. Install & Register
dotnet add package Foundatio.Mediator
// Program.cs
services.AddMediator();
2. Create Messages & Handlers
// Messages (records, classes, anything)
public record GetUser(int Id);
public record CreateUser(string Name, string Email);
public record UserCreated(int UserId, string Email);
// Handlers - just plain classes ending with "Handler" or "Consumer"
public class UserHandler
{
public async Task<Result<User>> HandleAsync(GetUser query, IUserRepository repo)
{
var user = await repo.FindAsync(query.Id);
return user ?? Result.NotFound($"User {query.Id} not found");
}
public async Task<(User user, UserCreated evt)> HandleAsync(CreateUser cmd, IUserRepository repo)
{
var user = new User { Name = cmd.Name, Email = cmd.Email };
await repo.AddAsync(user);
// Return tuple: first element is response, rest are auto-published
return (user, new UserCreated(user.Id, user.Email));
}
}
// Event handlers
public class EmailHandler
{
public async Task HandleAsync(UserCreated evt, IEmailService email)
{
await email.SendWelcomeAsync(evt.Email);
}
}
// Middleware - classes ending with "Middleware"
public class LoggingMiddleware(ILogger<LoggingMiddleware> logger)
{
public Stopwatch Before(object message) => Stopwatch.StartNew();
public void Finally(object message, Stopwatch sw, Exception? ex)
{
logger.LogInformation("Handled {MessageType} in {Ms}ms",
message.GetType().Name, sw.ElapsedMilliseconds);
}
}
3. Use the Mediator
// Query with response
var result = await mediator.InvokeAsync<Result<User>>(new GetUser(123));
if (result.IsSuccess)
Console.WriteLine($"Found user: {result.Value.Name}");
// Command with automatic event publishing
var user = await mediator.InvokeAsync<User>(new CreateUser("John", "john@example.com"));
// UserCreated event automatically published to EmailHandler
// Publish events to multiple handlers
await mediator.PublishAsync(new UserCreated(user.Id, user.Email));
๐ Learn More
Key topics:
- Getting Started - Step-by-step setup
- Handler Conventions - Discovery rules and patterns
- Middleware - Pipeline hooks and state management
- Result Types - Rich status handling
- Performance - Benchmarks vs other libraries
- Configuration - MSBuild and runtime options
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request. See our documentation for development guidelines.
๐ Related Projects
@martinothamar/Mediator was the primary source of inspiration for this library, but we wanted to use source interceptors and be conventional rather than requiring interfaces or base classes.
Other mediator and messaging libraries for .NET:
- MediatR - Simple, unambitious mediator implementation in .NET with request/response and notification patterns
- MassTransit - Distributed application framework for .NET with in-process mediator capabilities alongside service bus features
๐ License
MIT License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Foundatio.Mediator.Abstractions (>= 1.0.0-preview9)
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-preview9 | 246 | 9/16/2025 |
1.0.0-preview8 | 131 | 8/18/2025 |
1.0.0-preview7 | 132 | 8/14/2025 |
1.0.0-preview6 | 174 | 8/8/2025 |
1.0.0-preview5 | 152 | 8/8/2025 |
1.0.0-preview4 | 195 | 8/6/2025 |
1.0.0-preview3 | 191 | 8/5/2025 |
1.0.0-preview2 | 463 | 7/24/2025 |
1.0.0-preview12 | 45 | 9/21/2025 |
1.0.0-preview11 | 250 | 9/17/2025 |
1.0.0-preview10 | 253 | 9/16/2025 |
1.0.0-preview | 505 | 7/22/2025 |