Foundatio.Mediator.Abstractions
1.0.0-preview9
Prefix Reserved
dotnet add package Foundatio.Mediator.Abstractions --version 1.0.0-preview9
NuGet\Install-Package Foundatio.Mediator.Abstractions -Version 1.0.0-preview9
<PackageReference Include="Foundatio.Mediator.Abstractions" Version="1.0.0-preview9" />
<PackageVersion Include="Foundatio.Mediator.Abstractions" Version="1.0.0-preview9" />
<PackageReference Include="Foundatio.Mediator.Abstractions" />
paket add Foundatio.Mediator.Abstractions --version 1.0.0-preview9
#r "nuget: Foundatio.Mediator.Abstractions, 1.0.0-preview9"
#:package Foundatio.Mediator.Abstractions@1.0.0-preview9
#addin nuget:?package=Foundatio.Mediator.Abstractions&version=1.0.0-preview9&prerelease
#tool nuget:?package=Foundatio.Mediator.Abstractions&version=1.0.0-preview9&prerelease
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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- System.Diagnostics.DiagnosticSource (>= 9.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Foundatio.Mediator.Abstractions:
Package | Downloads |
---|---|
Foundatio.Mediator
A fast, convention-based C# mediator library using incremental source generators |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0-preview9 | 244 | 9/16/2025 |
1.0.0-preview8 | 133 | 8/18/2025 |
1.0.0-preview7 | 131 | 8/14/2025 |
1.0.0-preview6 | 172 | 8/8/2025 |
1.0.0-preview5 | 157 | 8/8/2025 |
1.0.0-preview4 | 197 | 8/6/2025 |
1.0.0-preview3 | 197 | 8/5/2025 |
1.0.0-preview12 | 47 | 9/21/2025 |
1.0.0-preview11 | 245 | 9/17/2025 |
1.0.0-preview10 | 252 | 9/16/2025 |
1.0.0-preview | 504 | 7/22/2025 |