FlowMediator 1.2.0
See the version list below for details.
dotnet add package FlowMediator --version 1.2.0
NuGet\Install-Package FlowMediator -Version 1.2.0
<PackageReference Include="FlowMediator" Version="1.2.0" />
<PackageVersion Include="FlowMediator" Version="1.2.0" />
<PackageReference Include="FlowMediator" />
paket add FlowMediator --version 1.2.0
#r "nuget: FlowMediator, 1.2.0"
#:package FlowMediator@1.2.0
#addin nuget:?package=FlowMediator&version=1.2.0
#tool nuget:?package=FlowMediator&version=1.2.0
FlowMediator
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!
Fork the repo
Create a feature branch (git checkout -b feature/my-feature)
Commit changes and push
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 | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.8)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.2.0 - Added Domain Event support with EF Core integration. Improved README and documentation.