AutomaticEasyCQRS 8.0.1

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

AutomaticEasyCQRS

O AutomaticEasyCQRS é uma biblioteca que visa facilitar a implementação do padrão CQRS (Command Query Responsibility Segregation) para desenvolvedores .NET. Com o AutomaticEasyCQRS, você pode criar e gerenciar facilmente os manipuladores de comandos, consultas e eventos, tornando a implementação do CQRS mais simples e organizada.

O que é CQRS?

O CQRS é um padrão arquitetural que separa a lógica de leitura (queries) da lógica de gravação (commands) em uma aplicação. Ele promove a divisão de responsabilidades, permitindo um design mais limpo e uma melhor escalabilidade do sistema.

Recursos

  • Fácil mapeamento de comandos, consultas e eventos.
  • Gerenciamento automático de manipuladores de CQRS.
  • Suporte a injeção de dependência.
  • Interface intuitiva para implementar manipuladores.

Como usar o AutomaticEasyCQRS?

Instalação

Para começar a usar o AutomaticEasyCQRS em seu projeto .NET, basta instalar o pacote NuGet:

dotnet add package AutomaticAutomaticEasyCQRS --version latest 

Ou ir diretamente no Nuget e pesquiar por AutomaticEasyCQRS.

Exemplo de uso

Veja como é fácil implementar um manipulador de comando, consulta e evento. Todos os Metodos podem ser Async ou não.

Commands

using System.Threading.Tasks;
using AutomaticEasyCQRS.Commands;

public class MyCommand : ICommand
{
    // Propriedades do comando
}

public class MyCommandHandler : ICommandHandler<MyCommand>
{
    public Task CommandHandle(MyCommand command)
    {
        // Lógica para manipular o comando
        return Task.CompletedTask;
    }
}

Queries

using System.Threading.Tasks;
using AutomaticEasyCQRS.Queries;

public class MyQuery : IQuery
{
    // Propriedades da Query
}

public class MyQueryResult : IQueryResult
{
    // Propriedades do Result
}

public class MyQueryHandler : IQueryHandler<MyQuery, MyQueryResult>
{
    public async Task<MyQueryResult> QueryHandle(MyQuery query)
    {
        // Lógica para manipular o comando
        return new MyQueryResult();
    }
}

Events

using System.Threading.Tasks;
using AutomaticEasyCQRS.Events;

public class Event : IEvent
{
    public Event(string userId)
    {
        UserId = userId;
    }
    public string UserId { get; }
}

public class EventHandler : IEventHandler<Event>
{
    public Task EventHandle(Event @event)
    {
        // Lógica para manipular o evento Event
        Console.WriteLine($"Usuário registrado com ID: {@event.UserId}");
        return Task.CompletedTask;
    }
}

Controllers

using AutomaticEasyCQRS.Bus.Command;
using AutomaticEasyCQRS.Bus.Event;
using AutomaticEasyCQRS.Bus.Query;

 public class TesteController : ControllerBase
    {
        public readonly ICommandBus commandBus;
        public readonly IQueryBus queryBus;
        public readonly IEventBus eventBus;


        public TesteController(ICommandBus commandBus, IQueryBus queryBus, IEventBus eventBus)
        {
            this.commandBus = commandBus;
            this.queryBus = queryBus;
            this.eventBus = eventBus;
        }

        [HttpGet]
        [Route("/teste/{q}")]
        public async Task<IActionResult> GetTest([FromRoute] string q)
        {

            // Command Syntax
            var handler = this.commandBus.Send(new TestCommand(q));
            // Query Syntax
            var queryHandler = this.queryBus.Query<TestQuery, TestResult>(new TestQuery(q));
            // Event Syntax
            var eventHandler = this.eventBus.Publish(new TestEvent(q));

            return Ok("Ok");
        }
    }

Configuração

Para registrar automaticamente os manipuladores de CQRS, adicione o seguinte código na classe Program.cs do .NET 6:

Agora o usuario pode selecionar o tipo de instancia que ele quer criar (Scoped, Transient ou Singleton).

public enum EHandlerInstanceType
    {
        Singleton,
        Scoped,
        Transient
    }

Por default é EHandlerInstanceType.Transient

using AutomaticEasyCQRS;
using Microsoft.AspNetCore.Diagnostics;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);
// Others Services
// Add services to the container.
...
CqrsBusRegistration.RegisterBuses(builder.Services, Assembly.GetExecutingAssembly(), EHandlerInstanceType.Scoped);

Telemetria

Para acessar a telemetria e salvar no banco de dados é necessario adicionar no seu mapeamento a classe

AutomaticEasyCQRS.Telemetry.TelemetryStatistics;

// Exemplo Entity Framework Core
public DbSet<TelemetryStatistics> TelemetryStatistics { get; set; }

Contribuindo

Contribuições são bem-vindas! Se você encontrar algum problema, tiver ideias para melhorias ou quiser contribuir com código, sinta-se à vontade para criar uma issue ou enviar um pull request.

Licença

Este projeto está licenciado sob a MIT License.

Contato

Se você tiver alguma dúvida ou precisar de suporte, entre em contato conosco pelo email: contato@jmdevelopment.dev.

Esperamos que o AutomaticEasyCQRS facilite a implementação do CQRS em seus projetos .NET. Aproveite e feliz codificação!

Artigos que contribuiram para que eu criasse o AutomaticEasyCQRS

https://dejanstojanovic.net/aspnet/2019/may/using-dispatcher-class-to-resolve-commands-and-queries-in-aspnet-core/ https://dejanstojanovic.net/aspnet/2019/may/automatic-cqrs-handler-registration-in-aspnet-core-with-reflection/ https://event-driven.io/en/how_to_register_all_mediatr_handlers_by_convention/

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
8.0.1 260 10/6/2025
8.0.0 202 7/17/2025
1.2.2 272 8/23/2023
1.2.1 250 7/26/2023
1.2.0 251 7/25/2023
1.1.0 247 7/25/2023
1.0.4 249 7/25/2023
1.0.3 249 7/21/2023
1.0.2 241 7/21/2023

Correção QueryBus Throw