PabloDispatch 1.1.0-alpha

This is a prerelease version of PabloDispatch.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package PabloDispatch --version 1.1.0-alpha
NuGet\Install-Package PabloDispatch -Version 1.1.0-alpha
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="PabloDispatch" Version="1.1.0-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PabloDispatch --version 1.1.0-alpha
#r "nuget: PabloDispatch, 1.1.0-alpha"
#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.
// Install PabloDispatch as a Cake Addin
#addin nuget:?package=PabloDispatch&version=1.1.0-alpha&prerelease

// Install PabloDispatch as a Cake Tool
#tool nuget:?package=PabloDispatch&version=1.1.0-alpha&prerelease

PabloDispatch

Introduction

PabloDispatch is a C# library designed to simplify and implement the Command Query Responsibility Segregation (CQRS) pattern in your application. CQRS is a design pattern that separates the read and write operations, treating them as distinct responsibilities.

Table of content

How PabloDispatch Works

PabloDispatch provides a set of interfaces and components that help you effectively organize your application using CQRS principles. Here's an overview of key components:

  1. Commands/Queries and Handlers: The library includes marker interfaces IQuery and ICommand representing requests with and without a return type, respectively. It also defines IQueryHandler<TQuery, TResponse> and ICommandHandler<TCommand> interfaces to define handlers for processing these requests.

  2. Pipeline Handlers: PabloDispatch provides ICommandPipelineHandler<TCommand> and IQueryPipelineHandler<TQuery> interfaces for pre- and post-processing of requests. These pipeline handlers allow you to add additional behavior before and after the main request is processed.

  3. Dispatcher: The IDispatcher interface represents a command and query dispatcher. It is responsible for identifying the appropriate handler for a given request and dispatching it for processing.

How to Use PabloDispatch

Registration

To start using the PabloDispatch library, you need to register its components in your application's dependency injection container. Here's how you can do it:

// In your startup or composition root class:
public void ConfigureServices(IServiceCollection services)
{
    // Register the PabloDispatch components using the extension method.
    services.AddPabloDispatch(component =>
    {
        // Registering a command handler:
        component.SetCommandHandler<YourCommandType, YourCommandHandlerType>();

        // Registering a query handler:
        component.SetQueryHandler<YourQueryType, YourReturnType, YourQueryHandlerType>();

        // Registering a command handler with pre- and post-processing:
        component.SetCommandHandler<YourCommandType, YourCommandHandlerType>(pipeline =>
        {
            pipeline
                .AddPreProcessor<YourCommandPipelineHandlerType>()
                .AddPostProcessor<YourCommandPipelineHandlerType>();
        });
    });
}

Usage example

Here is a simple example of how to implement an IQueryHandler to fetch a customer by email

  • First, implement a query of IQuery
public class FetchCustomerByEmailQuery : IQuery
{
    public string Email { get; set; }

    public FetchCustomerByEmailQuery(string email)
    {
        Email = email;
    }
}
  • Then implement the IQueryHandler<FetchCustomerByEmailQuery, Customer> query handler. This is where we want to centralize the logic.
public class FetchCustomerByEmailQueryHandler : IQueryHandler<FetchCustomerByEmailQuery, Customer>
{
    private readonly ICustomerRepository _customerRepository;

    public FetchCustomerByEmailQueryHandler(ICustomerRepository customerRepository)
    {
        _customerRepository = customerRepository;
    }

    public async Task<Customer> HandleAsync(FetchCustomerByEmailQuery query, CancellationToken cancellationToken = default)
    {
        var customer = await _customerRepository.FirstOrDefault(x => x.Email == query.Email);

        return customer;
    }
}
  • Lastly, the IDispatcher interface can be used to fire the FetchCustomerByEmailQuery and the Customer will be returned.
public class SomeClass
{
    private readonly IDispatcher _dispatcher;

    public SomeClass(IDispatcher dispatcher)
    {
        _dispatcher = dispatcher;
    }

    public async Task SomeMethod(string customerEmail)
    {
        var query = new FetchCustomerByEmailQuery(customerEmail);

        var customer = await _dispatcher.DispatchAsync<FetchCustomerByEmailQuery, Customer>(query);

        // Rest of method
    }
}

Remember to set the query and the query handler as specified here

Note on Alpha Version

Please be aware that PabloDispatch is currently in the alpha stage, which means it's still undergoing early testing and development. As a result, changes may occur in subsequent releases as I refine and improve the library.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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
2.0.0-alpha 112 12/26/2023
1.1.0-alpha 81 7/26/2023
0.1.0-alpha 76 7/18/2023