FlowMediator 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FlowMediator --version 1.2.0
                    
NuGet\Install-Package FlowMediator -Version 1.2.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="FlowMediator" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FlowMediator" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="FlowMediator" />
                    
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 FlowMediator --version 1.2.0
                    
#r "nuget: FlowMediator, 1.2.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 FlowMediator@1.2.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=FlowMediator&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=FlowMediator&version=1.2.0
                    
Install as a Cake Tool

FlowMediator

NuGet NuGet Downloads Build

link: https://www.nuget.org/packages/FlowMediator/

Overview

FlowMediator is a lightweight mediator library for .NET 8/9, designed to simplify the CQRS and Mediator pattern with minimal setup. It supports request/response messaging, pipeline behaviors, and domain events out of the box.

Use it when you want:

-Clean separation of concerns in your application

-Simple request/response messaging without boilerplate

-EF Core integration with Domain Events

-A minimal learning-friendly alternative to heavy frameworks

Features

✅ Request/Response messaging (IRequest<TResponse>, IRequestHandler<TRequest,TResponse>)

✅ Pipeline behaviors (IPipelineBehavior<TRequest,TResponse>) for logging, validation, etc.

✅ Domain Events with EF Core integration (v1.2.0+)

✅ Dependency Injection (DI) extensions for IServiceCollection

✅ Manual or automatic pipeline registration

✅ .NET 8 and .NET 9 support

Installation

dotnet add package FlowMediator --version 1.2.0

Example Usage

1.Define a Request & Handler

// Query
public record GetUserByIdQuery(int Id) : IRequest<UserDto>;

// Handler
public class GetUserByIdQueryHandler : IRequestHandler<GetUserByIdQuery, UserDto>
{
    private readonly IUserRepository _repository;

    public GetUserByIdQueryHandler(IUserRepository repository)
    {
        _repository = repository;
    }

    public async Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
    {
        var user = await _repository.GetByIdAsync(request.Id);
        return user ?? throw new Exception("User not found");
    }
}

2. Register in DI

services.AddFlowMediator(typeof(GetUserByIdQuery).Assembly);
services.AddScoped<IUserRepository, UserRepository>();

// Optional: pipeline behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));

3. Use Mediator

var mediator = provider.GetRequiredService<IMediator>();

var user = await mediator.Send(new GetUserByIdQuery(1));
Console.WriteLine(user.Name); // "User 1"

Domain Events with EF Core

Base Entity

public abstract class BaseEntity
{
    private readonly List<IRequest<Unit>> _domainEvents = new();
    public IReadOnlyCollection<IRequest<Unit>> DomainEvents => _domainEvents.AsReadOnly();

    protected void AddDomainEvent(IRequest<Unit> domainEvent) => _domainEvents.Add(domainEvent);
    public void ClearDomainEvents() => _domainEvents.Clear();
}

DbContext Integration

public class AppDbContext : DbContext
{
    private readonly IMediator _mediator;

    public AppDbContext(DbContextOptions<AppDbContext> options, IMediator mediator)
        : base(options)
    {
        _mediator = mediator;
    }

    public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        var entities = ChangeTracker
            .Entries<BaseEntity>()
            .Where(e => e.Entity.DomainEvents.Any())
            .Select(e => e.Entity);

        foreach (var entity in entities)
        {
            foreach (var domainEvent in entity.DomainEvents)
            {
                await _mediator.Publish(domainEvent);
            }
            entity.ClearDomainEvents();
        }

        return await base.SaveChangesAsync(cancellationToken);
    }
}

Example Entity + Event

public class User : BaseEntity
{
    public Guid Id { get; private set; }
    public string Name { get; private set; }

    public User(string name)
    {
        Id = Guid.NewGuid();
        Name = name;
        AddDomainEvent(new UserCreatedEvent(Id, Name));
    }
}

public record UserCreatedEvent(Guid Id, string Name) : IRequest<Unit>;

Event Handler

public class UserCreatedEventHandler : IRequestHandler<UserCreatedEvent, Unit>
{
    public Task<Unit> Handle(UserCreatedEvent notification, CancellationToken cancellationToken)
    {
        Console.WriteLine($"[DomainEvent] User created: {notification.Name}");
        return Task.FromResult(Unit.Value);
    }
}


🤝 Contributing

Contributions, bug reports, and feature requests are welcome!

  1. Fork the repo

  2. Create a feature branch (git checkout -b feature/my-feature)

  3. Commit changes and push

  4. Open a Pull Request

⚠️ Disclaimer

FlowMediator is a lightweight mediator library designed for hobby projects, learning, and simple applications.
It does not implement any security features by itself.
Please handle validation, authorization, and exception management within your own application.

📜 License

Licensed under the MIT License. See LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 is compatible.  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. 
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.1 112 1/26/2026
1.2.0 207 9/25/2025
1.0.0 252 8/26/2025

v1.2.0 - Added Domain Event support with EF Core integration. Improved README and documentation.