Foundatio.Mediator 1.0.0-preview8

Prefix Reserved
This is a prerelease version of Foundatio.Mediator.
dotnet add package Foundatio.Mediator --version 1.0.0-preview8
                    
NuGet\Install-Package Foundatio.Mediator -Version 1.0.0-preview8
                    
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-preview8" />
                    
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-preview8" />
                    
Directory.Packages.props
<PackageReference Include="Foundatio.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 Foundatio.Mediator --version 1.0.0-preview8
                    
#r "nuget: Foundatio.Mediator, 1.0.0-preview8"
                    
#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-preview8
                    
#: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-preview8&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Foundatio.Mediator&version=1.0.0-preview8&prerelease
                    
Install as a Cake Tool

FoundatioFoundatio

Build status NuGet Version feedz.io Discord

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

๐Ÿ‘‰ Complete Documentation

Key topics:

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. See our documentation for development guidelines.

๐Ÿ“„ License

MIT License

Product 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.

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-preview8 122 8/18/2025
1.0.0-preview7 128 8/14/2025
1.0.0-preview6 170 8/8/2025
1.0.0-preview5 148 8/8/2025
1.0.0-preview4 192 8/6/2025
1.0.0-preview3 188 8/5/2025
1.0.0-preview2 440 7/24/2025
1.0.0-preview 482 7/22/2025