Franz.Common.Mediator 1.3.0

There is a newer version of this package available.
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
                    
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="Franz.Common.Mediator" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Franz.Common.Mediator" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Franz.Common.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 Franz.Common.Mediator --version 1.3.0
                    
#r "nuget: Franz.Common.Mediator, 1.3.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 Franz.Common.Mediator@1.3.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=Franz.Common.Mediator&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Franz.Common.Mediator&version=1.3.0
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.5.3 44 9/21/2025
1.5.2 41 9/21/2025
1.5.0 44 9/21/2025
1.4.4 88 9/20/2025
1.3.14 242 9/18/2025
1.3.13 242 9/18/2025
1.3.5 266 9/17/2025
1.3.4 306 9/16/2025
1.3.3 315 9/16/2025
1.3.2 310 9/15/2025
1.3.1 124 9/12/2025
1.3.0 331 8/25/2025