Allegro.Extensions.Cqrs.Abstractions 2.0.0

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

// Install Allegro.Extensions.Cqrs.Abstractions as a Cake Tool
#tool nuget:?package=Allegro.Extensions.Cqrs.Abstractions&version=2.0.0                

Allegro.Extensions.Cqrs

Believe that there is no need to describe it once more and point you to authority:

https://martinfowler.com/bliki/CQRS.html

In this package our custom implementation of tools and markers are delivered.

Allegro.Extensions.Cqrs.Abstractions

Contains common CQRS set of markers and abstractions like Command, Query<>, ICommandDispatcher, IQueryDispatcher, ICommandHandler, IQueryHandler.

Additionally we introduce some additional things like Command and Query Validators, or Fluent Validations.

ICommandValidator and IQueryValidator

In more sophisticated validation cases, that simple DataAnnotations are not enough we introduce ICommandValidator<ICommand> and IQueryValidator<IQuery> to enables adding some validation logic before command or query execution.

internal class BarCommandValidator : ICommandValidator<BarCommand>
{
    public Task Validate(BarCommand command)
    {
       // validation logic
    }
}
internal class BarQueryValidator : IQueryValidator<BarQuery>
{
    public Task Validate(BarQuery query, CancellationToken cancellationToken)
    {
       // validation logic
    }
}

Sample

Fluent validations

You can use AddCqrsFluentValidations from package Allegro.Extensions.Cqrs.FluentValidations to use Fluent Validations instead proposed interfaces.

services.AddCqrsFluentValidations(cqrsAssemblies)

internal class FooCommandFluentValidator : AbstractValidator<FooCommand>
{
    public FooCommandFluentValidator()
    {
        RuleFor(_ => _.Name).NotEmpty();
    }
}

Sample

Decorators

This give opportunity to Decorate handlers with any custom code. Remember to add Decorator attribute to your decorator.
Thanks to it, it will be excluded from auto-registration of handlers and does not loop 😉

[Decorator]
internal class BarCommandHandlerDecorator : ICommandHandler<BarCommand>
{
    private readonly ICommandHandler<BarCommand> _decorated;
    private readonly ILogger<BarCommand> _logger;

    public BarCommandHandlerDecorator(ICommandHandler<BarCommand> decorated, ILogger<BarCommand> logger)
    {
        _decorated = decorated;
        _logger = logger;
    }

    public async Task Handle(BarCommand command)
    {
        _logger.LogInformation("Before handle");
        await _decorated.Handle(command);
        _logger.LogInformation("After handle");
    }
}
[Decorator]
internal class BarQueryHandlerDecorator : IQueryHandler<BarQuery, BarData>
{
    private readonly IQueryHandler<BarQuery, BarData> _decorated;
    private readonly ILogger<BarCommand> _logger;

    public BarQueryHandlerDecorator(IQueryHandler<BarQuery, BarData> decorated, ILogger<BarCommand> logger)
    {
        _decorated = decorated;
        _logger = logger;
    }

    public async Task<BarData> Handle(BarQuery query, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Before handle");
        var result = await _decorated.Handle(query, cancellationToken);
        _logger.LogInformation("After handle");
        return result;
    }
}

Registration with Scrutor:

    services.TryDecorate<ICommandHandler<BarCommand>, BarCommandHandlerDecorator>();
    services.TryDecorate<IQueryHandler<BarQuery, BarData>, BarQueryHandlerDecorator>();

Remember to first register all commands handlers and than register custom decorator. Execution order is LIFO to registration order.

Samples

Some sample usage could be found:

Allegro.Extensions.Cqrs

This package contains:

  • default implementation of ICommandDispatcher and IQueryDispatcher
  • automatic registrations of all ICommandHandler, IQueryHandler, ICommandValidator

For registrations Scrutor packages is used as a tool.

Why not MediatR?

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

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Allegro.Extensions.Cqrs.Abstractions:

Package Downloads
Allegro.Extensions.Cqrs

Contains Cqrs defualt implementation of automatic registration, default dispatchers and command actions support used in Allegro services

Allegro.Extensions.Cqrs.FluentValidations

Contains FluentVlidations extension for Allegro.Extensions.Cqrs

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 172 8/23/2024
2.0.0 506 1/27/2023
1.1.0 506 11/22/2022
1.0.0 439 11/10/2022