FastMediator 0.0.5

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

FastMediator

NuGet NuGet License

FastMediator è un'implementazione leggera e performante del pattern Mediator per .NET, ottimizzata per prestazioni e facilità d'uso. Progettata per applicazioni che necessitano di un elevato throughput con un overhead minimo, permette di disaccoppiare i componenti dell'applicazione implementando il CQRS (Command Query Responsibility Segregation) in modo semplice ed elegante.

Caratteristiche

  • 🚀 Alte prestazioni: Utilizza espressioni compilate e caching dei delegati per ottenere prestazioni ottimali
  • 🧩 Supporto CQRS: Separazione netta tra comandi (richieste che modificano lo stato) e query (richieste che restituiscono dati)
  • 🔄 Pipeline di comportamenti: Possibilità di intercettare richieste con comportamenti configurabili come validazione, logging e misurazione delle prestazioni
  • 📢 Sistema di notifiche: Supporto per il pattern publish/subscribe con notifiche a più handler
  • 🔍 Diagnostica integrata: Funzionalità di logging dettagliato e misurazione delle prestazioni per semplificare il debug e l'ottimizzazione
  • Validazione integrata: Validazione delle richieste in ingresso prima dell'elaborazione
  • 🧰 Configurazione semplice: Integrazione perfetta con Microsoft.Extensions.DependencyInjection

Installazione

dotnet add package FastMediator

Configurazione

Configura FastMediator nel tuo container IoC:

services.AddCustomMediator(scan => scan.FromAssemblyOf<Program>(), options =>
{
    options.EnableDiagnostics = true;    // Abilita behavior diagnostici
    options.EnableTiming = true;         // Abilita misurazione tempi
    options.EnableDetailedLogging = true; // Abilita logging dettagliato
    options.LoggerFactory = loggerFactory; // Facoltativo: factory per i logger
});

Utilizzo Base

1. Definisci una Richiesta e il suo Handler

// Richiesta
public class Ping : IRequest<string>
{
    public string Message { get; }
    
    public Ping(string message)
    {
        Message = message;
    }
}

// Handler
public class PingHandler : IRequestHandler<Ping, string>
{
    public string Handle(Ping request)
    {
        return $"Risposta a: {request.Message}";
    }
}

2. Invia la Richiesta

// Inietta il dispatcher
public class MyService
{
    private readonly Dispatcher _mediator;
    
    public MyService(Dispatcher mediator)
    {
        _mediator = mediator;
    }
    
    public void ProcessMessage(string message)
    {
        // Invia la richiesta e ottieni la risposta
        string response = _mediator.Send(new Ping(message));
        Console.WriteLine(response);
    }
}

Notifiche

Le notifiche permettono la pubblicazione di eventi a più handler.

1. Definisci una Notifica e i suoi Handler

// Notifica
public class SomethingHappened : INotification
{
    public string Message { get; set; }
}

// Handler
public class SomethingHappenedHandler : INotificationHandler<SomethingHappened>
{
    public void Handle(SomethingHappened notification)
    {
        Console.WriteLine($"Evento gestito: {notification.Message}");
    }
}

// Puoi avere più handler per la stessa notifica
public class AnotherSomethingHappenedHandler : INotificationHandler<SomethingHappened>
{
    public void Handle(SomethingHappened notification)
    {
        // Fai qualcos'altro con la notifica
    }
}

2. Pubblica la Notifica

_mediator.Publish(new SomethingHappened { Message = "Un evento importante è accaduto!" });

Pipeline di Behaviors

I behaviors permettono di intercettare e manipolare le richieste prima che raggiungano l'handler.

Behavior Inclusi

FastMediator include diversi behaviors pronti all'uso:

  • ValidationBehavior: Valida le richieste prima dell'elaborazione
  • LoggingBehavior: Registra dettagli delle richieste e delle risposte
  • TimingBehavior: Misura il tempo di elaborazione delle richieste
  • DiagnosticBehavior: Fornisce informazioni sulla pipeline di behaviors

Creazione di un Behavior Personalizzato

public class MyCustomBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>, IOrderedPipelineBehavior
    where TRequest : IRequest<TResponse>
{
    // Priorità di esecuzione (più basso = più alta priorità)
    public int Order => 100;
    
    public TResponse Handle(TRequest request, Func<TRequest, TResponse> next)
    {
        // Logica pre-elaborazione
        Console.WriteLine($"Pre-elaborazione per {typeof(TRequest).Name}");
        
        // Chiama il prossimo handler nella pipeline
        var response = next(request);
        
        // Logica post-elaborazione
        Console.WriteLine($"Post-elaborazione per {typeof(TRequest).Name}");
        
        return response;
    }
}

// Registralo nel container
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(MyCustomBehavior<,>));

Validazione

FastMediator include un sistema di validazione integrato.

1. Crea un Validatore

public class CreateUserValidator : AbstractValidator<CreateUserCommand>
{
    protected override void ValidateInternal(CreateUserCommand request, ValidationResult result)
    {
        if (string.IsNullOrEmpty(request.Username))
            result.AddError(nameof(request.Username), "Username è obbligatorio");
            
        if (request.Password?.Length < 8)
            result.AddError(nameof(request.Password), "La password deve contenere almeno 8 caratteri");
    }
}

2. Registralo nel Container IoC

Il validatore viene registrato automaticamente se utilizzi il metodo di scansione dell'assembly.

Esempi Avanzati

CQRS con Differenti Tipo di Richieste

// Query (restituzione dati senza modifiche di stato)
public class GetUserQuery : IRequest<UserDto>
{
    public int UserId { get; set; }
}

// Command (modifica lo stato)
public class CreateUserCommand : IRequest<int>
{
    public string Username { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

Logging e Diagnostica

var stats = mediator.GetRequestHandlerCacheStats();
Console.WriteLine($"Cache hits: {stats.Hits}, misses: {stats.Misses}");
Console.WriteLine($"Cache size: {mediator.RequestHandlerCacheSize}");

Performance

FastMediator è progettato per alte prestazioni:

  • Utilizza espressioni compilate per generare delegati fortemente tipizzati
  • Memorizza nella cache i delegati generati per minimizzare l'overhead di reflection
  • Implementa una pipeline di comportamenti ottimizzata
  • Supporta l'ordinamento esplicito dei behaviors per un controllo preciso del flusso di esecuzione

Best Practices

  1. Tenere le richieste immutabili: Definisci le proprietà come readonly o utilizza record C#
  2. Usare tipi di ritorno appropriati: Ritorna void o Task per comandi, dati specifici per query
  3. Separare richieste e handler: Mantieni ciascun handler in un file separato per migliorare l'organizzazione del codice
  4. Utilizzare behavior per preoccupazioni trasversali: Validazione, logging, caching, ecc.
  5. Ordinare i behaviors correttamente: Usa l'interfaccia IOrderedPipelineBehavior per controllare l'ordine di esecuzione

Contribuire

Le contribuzioni sono benvenute! Se desideri migliorare FastMediator, sentiti libero di inviare una pull request.

Licenza

FastMediator è distribuito sotto la licenza MIT. Vedi il file LICENSE per maggiori dettagli.

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 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. 
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 161 4/22/2025
0.0.7 140 4/18/2025
0.0.6 198 4/16/2025
0.0.5 194 4/15/2025
0.0.4 193 4/15/2025
0.0.3 193 4/15/2025
0.0.2 187 4/15/2025
0.0.1 190 4/15/2025