Dsptch 1.1.0

dotnet add package Dsptch --version 1.1.0
                    
NuGet\Install-Package Dsptch -Version 1.1.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="Dsptch" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dsptch" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Dsptch" />
                    
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 Dsptch --version 1.1.0
                    
#r "nuget: Dsptch, 1.1.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 Dsptch@1.1.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=Dsptch&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Dsptch&version=1.1.0
                    
Install as a Cake Tool

DSPTCH

Lightweight library for implementing CQRS and event dispatching in .NET.

Installation

Install the package from NuGet.

dotnet add package DSPTCH

CQRS

Create a request, query or command by implementing the IRequest, IQuery or ICommand interface.

public record GetProductByIdQuery(Guid Id)
    : IQuery<Product>;

public record GetProductByNameQuery(string Name)
    : IRequest<List<Product>>;

public record CreateProductCommand(string Name, decimal Price)
    : ICommand<Guid>;

IQuery and ICommand inherit from IRequest.

Handlers

Create a handler for each request, query or command by implementing the IRequestHandler interface.

public class GetProductByIdQueryHandler(IProductRepository productRepository)
    : IRequestHandler<GetProductByIdQuery, Product?>
{
    public Task<Product?> Handle(GetProductByIdQuery query, CancellationToken cancellationToken)
    {
        return productsRepository.GetById(query.Id);
    }
}

Dispatch

Dispatch the request, query or command using the IDispather interface.

app.MapGet("/products/{id}", async (IDispatcher dispatcher, Guid id) =>
{
    Product? product = await dispatcher.Dispatch<GetProductByIdQuery, Product?>(new GetProductByIdQuery(id));

    return product is not null ? Results.Ok(product) : Results.NotFound();
});

Dispatch decorators

Use dispatch decorators to add cross-cutting concerns to the dispatching process.

// This decorator will be called before and after every request is dispatched
public class LoggingDecorator<TRequest, TResult>(ILogger<TRequest> logger)
    : IDispatcherDecorator<TRequest, TResult>
        where TRequest : IRequest<TResult>
{
    public async Task<TResult> Dispatch(
        TRequest command,
        RequestHandlerDelegate<TResult> handler,
        CancellationToken cancellationToken = default)
    {
        var requestName = typeof(TRequest).Name;
        logger.LogInformation("Dispatching request: {req}", requestName);

        try
        {
            var result = await handler();

            logger.LogInformation("Request dispatched: {req}", requestName);

            return result;
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "Error dispatching request: {req}", requestName);
            throw;
        }
    }
}

// This decorator will be called before and after every query is dispatched
// It won't be called for IRequestHandler or ICommandHandler implementations
public class CachingDecorator<TQuery, TResult>(
    IMemoryCache cache,
    ILogger<CachingDecorator<TQuery, TResult>> logger)
        : IDispatcherDecorator<TQuery, TResult>
            where TQuery : IQuery<TResult>
{
}

// This decorator will only be called before and after every IAuthorizableRequest is dispatched
public class AuthorizationDecorator<TQuery, TResult>
    : IDispatcherDecorator<TQuery, TResult>
        where TQuery : IAuthorizableRequest<TResult>
{
}

Dependency Injection

Use Microsoft DI to register the dispatcher and handlers.

builder.Services.AddDsptch(opts =>
{
    opts.RegisterServicesFromAssemblyContaining<Program>();

    // Register dispatchers using DsptchConfiguration
    opts.RegisterDispatcherDecorator(typeof(LoggingDecorator<,>));
    opts.RegisterDispatcherDecorator(typeof(CachingDecorator<,>));
});

// Or register dispatchers manually
builder.Services.TryAddTransient(typeof(IDispatcherDecorator<,>), typeof(LoggingDecorator<,>));
builder.Services.TryAddTransient(typeof(IDispatcherDecorator<,>), typeof(CachingDecorator<,>));
  • The AddDsptch method will register the dispatcher and handlers in the specified assemblies.
    • They will be registered as transient services by default, but this can be changed via opts.Lifetime.
  • The decorators can be registered using the RegisterDispatcherDecorator method.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.1.0 211 5/4/2024
1.0.0 170 5/4/2024