Franz.Common.Mediator
1.3.0
See the version list below for details.
dotnet add package Franz.Common.Mediator --version 1.3.0
NuGet\Install-Package Franz.Common.Mediator -Version 1.3.0
<PackageReference Include="Franz.Common.Mediator" Version="1.3.0" />
<PackageVersion Include="Franz.Common.Mediator" Version="1.3.0" />
<PackageReference Include="Franz.Common.Mediator" />
paket add Franz.Common.Mediator --version 1.3.0
#r "nuget: Franz.Common.Mediator, 1.3.0"
#:package Franz.Common.Mediator@1.3.0
#addin nuget:?package=Franz.Common.Mediator&version=1.3.0
#tool nuget:?package=Franz.Common.Mediator&version=1.3.0
Franz.Common.Mediator
Franz.Common.Mediator
is the core mediator library for the Franz framework.
It provides request/response handling, notifications, and pipeline behaviors, fully decoupled from business models and infrastructure.
๐ Project Structure
Franz.Common/
โโโ Franz.Common.Business/ # Domain models, ValueObjects, Commands, Queries
โโโ Franz.Common.Mediator/ # Mediator abstractions, dispatcher, pipelines, handlers
โ โโโ Messages/ # ICommand, IQuery, INotification definitions
โ โโโ Handlers/ # ICommandHandler, IQueryHandler, INotificationHandler
โ โโโ Pipelines/ # IPipeline and custom pipeline behaviors
โ โโโ Dispatchers/ # Dispatcher implementing Send/Publish
โโโ Franz.Common.Infrastructure/ # Handler implementations, database or messaging integrations
โโโ Franz.Common.Tests/ # Unit tests for Business and Mediator layers
โก Usage
1. Define Commands and Queries (Business Layer)
using Franz.Common.Mediator.Messages;
public record CreateUserCommand(string Name, string Email) : ICommand<Guid>;
public record GetUserQuery(Guid Id) : IQuery<User>;
2. Implement Handlers (Infrastructure Layer)
using Franz.Common.Mediator.Handlers;
using Franz.Common.Business.Domain;
public class CreateUserHandler : ICommandHandler<CreateUserCommand, Guid>
{
private readonly IUserRepository _repository;
public CreateUserHandler(IUserRepository repository)
=> _repository = repository;
public async Task<Guid> Handle(CreateUserCommand request, CancellationToken cancellationToken)
{
var user = new User(request.Name, request.Email);
await _repository.AddAsync(user);
return user.Id;
}
}
public class GetUserHandler : IQueryHandler<GetUserQuery, User>
{
private readonly IUserRepository _repository;
public GetUserHandler(IUserRepository repository)
=> _repository = repository;
public Task<User> Handle(GetUserQuery request, CancellationToken cancellationToken)
=> _repository.GetByIdAsync(request.Id);
}
3. Register Mediator in DI Container
using Microsoft.Extensions.DependencyInjection;
using Franz.Common.Mediator;
var services = new ServiceCollection();
services
.AddMediator(typeof(Dispatcher).Assembly) // Registers pipelines and handlers
.AddDependencies(); // Add business and infrastructure dependencies
4. Send Commands and Queries
var dispatcher = serviceProvider.GetRequiredService<IDispatcher>();
// Send a command
var userId = await dispatcher.Send(new CreateUserCommand("Alice", "alice@example.com"));
// Send a query
var user = await dispatcher.Send(new GetUserQuery(userId));
๐งฉ Pipeline Behaviors
Custom behaviors can be applied globally:
using Franz.Common.Mediator.Pipelines;
using Franz.Common.EntityFramework.Behaviors;
services.AddTransient(typeof(IPipeline<,>), typeof(PersistenceBehavior<,>));
services.AddTransient(typeof(IPipeline<,>), typeof(LoggingBehavior<,>));
services.AddTransient(typeof(IPipeline<,>), typeof(ValidationBehavior<,>));
๐ข Notifications (Integration Events)
using Franz.Common.Mediator.Messages;
public record UserCreatedEvent(User User) : INotification;
public class UserCreatedHandler : INotificationHandler<UserCreatedEvent>
{
public Task Handle(UserCreatedEvent notification, CancellationToken cancellationToken)
{
Console.WriteLine($"User created: {notification.User.Id}");
return Task.CompletedTask;
}
}
// Publishing a notification
await dispatcher.Publish(new UserCreatedEvent(user));
๐งช Unit Tests
All existing unit tests in Franz.Common.Business.Tests
and Franz.Common.Mediator.Tests
ensure value objects, commands/queries, and notifications behave correctly:
[Fact]
public void Copy_SameValueObjectAfter_ReturnsTrue()
{
var addresseCopie = AddresseUnique.GetCopy();
Assert.True(addresseCopie.Equals(AddresseUnique));
}
Run all tests:
dotnet test
๐ง Integration
- Dispatcher resolves handlers from DI automatically.
- Pipelines (logging, validation, persistence) are fully pluggable.
- Supports commands, queries, and notifications in a clean separation of concerns.
- Independent of MediatR โ Franz.Common.Mediator is the authoritative mediator layer.
๐ License
MIT
flowchart TD
subgraph Business
A[Commands / Queries / Events] -->|Send / Publish| B[Dispatcher / IDispatcher]
end
subgraph Mediator
B --> C[Pipeline Behaviors]
C --> D[Handler Resolution]
end
subgraph Infrastructure
D --> E[Command / Query Handler Implementations]
D --> F[Notification Handlers / Integration Events]
E --> G[Repositories / Database / External Services]
F --> H[Messaging / Event Publishing]
end
style Business fill:#f9f,stroke:#333,stroke-width:1px
style Mediator fill:#9cf,stroke:#333,stroke-width:1px
style Infrastructure fill:#fc9,stroke:#333,stroke-width:1px
๐ Changelog
Version 1.3
- Upgraded to .NET 9.0.8
- Added new features and improvements
- Separated business concepts from mediator concepts
- Now compatible with both the in-house mediator and MediatR
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Franz.Common.Mediator:
Package | Downloads |
---|---|
Franz.Common.Business
Shared utility library for the Franz Framework. |
|
Franz.Common.Bootstrap
Shared utility library for the Franz Framework. |
|
Franz.Common.Http.Refit
Shared utility library for the Franz Framework. |
|
Franz.Common.Mediator.Polly
Shared utility library for the Franz Framework. |
|
Franz.Common.Caching
Shared utility library for the Franz Framework. |
GitHub repositories
This package is not used by any popular GitHub repositories.