QuokkaDev.Cqrs 3.1.0-alpha0

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

// Install QuokkaDev.Cqrs as a Cake Tool
#tool nuget:?package=QuokkaDev.Cqrs&version=3.1.0-alpha0&prerelease                

Quality Gate Status Coverage Maintainability Rating Technical Debt Duplicated Lines (%) publish workflow

QuokkaDev.CQRS

A package for apply CQRS pattern in .NET projects and add cross cutting concerns

QuokkaDev.CQRS is a wrap around MediatR. According to CQRS Design Pattern it allow a clear distinction between commands and query. It is inspired by this article

Installing QuokkaDev.Cqrs

You should install the packages via the .NET Core command line interface:

dotnet add package QuokkaDev.Cqrs    

The commands, will download and install QuokkaDev.Cqrsand all required dependencies.

Register handlers in D.I. Container

Call the extension method IServiceCollection.AddCQRS() passing an array of assemblies to scan for search command and query handlers. If no assembly are passed to the extension method the current executing assembly will be used

program.cs
using QuokkaDev.Cqrs;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);

// Configure services
// ...

//Add CQRS infrastructure.
builder.Services.AddCQRS(Assembly.GetExecutingAssembly());


var app = builder.Build();

// Configure middleware

app.Run();

Commands

Create the command and the result

OrderCreate.cs
using QuokkaDev.Cqrs;

namespace QuokkaDevCqrsDemoApi
{
    public class OrderCreateCommand
    {
        public string CustomerName { get; set; } = "";
        public int ItemId { get; set; }
        public decimal ItemQuantity { get; set; }
    }

    public class OrderCreateResult
    {
        public int Id { get; set; }
        public DateTime Created { get; set; }
    }
}

Create an handler for the command

OrderCreateCommandHandler.cs
using QuokkaDev.Cqrs;

namespace QuokkaDevCqrsDemoApi
{
    public class OrderCreateCommandHandler : ICommandHandler<OrderCreateCommand, OrderCreateResult>
    {
        public Task<OrderCreateResult> Handle(OrderCreateCommand request, CancellationToken cancellationToken)
        {
            // Create the order and persist it on database
            // ...
            // ...

            return Task.FromResult(new OrderCreateResult() { Id = 1, Created = DateTime.Now });
        }
    }
}

Use the CommandDispatcher for dispatch commands to the right handler

OrderController.cs
using QuokkaDev.Cqrs;
using Microsoft.AspNetCore.Mvc;

namespace QuokkaDevCqrsDemoApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly ICommandDispatcher commandDispatcher;

        public OrderController(ICommandDispatcher commandDispatcher)
        {
            this.commandDispatcher = commandDispatcher;
        }

        [HttpPost]
        public async Task<IActionResult> CreateOrder([FromBody]OrderCreateCommand orderCreateCommand)
        {
            OrderCreateResult result = await commandDispatcher.Dispatch<OrderCreateCommand, OrderCreateResult>(orderCreateCommand);
            return Ok(result);
        }
    }
}

Queries

Create the query and the result

OrderCreate.cs
using QuokkaDev.CQRS;

namespace QuokkaDevCqrsDemoApi
{
    public class AllOrdersQuery
    {
    }

    public class AllOrdersQueryResult
    {
        public OrderDTO[]? OrderDTOs { get; set; }
    }

    public class OrderDTO
    {
        public int Id { get; set; }
        public string Customer { get; set; } = "";
    }
}

Create an handler for the query

AllOrdersQueryHandler.cs
using QuokkaDev.Cqrs;

namespace QuokkaDevCqrsDemoApi
{
    public class AllOrdersQueryHandler : IQueryHandler<AllOrdersQuery, AllOrdersQueryResult>
    {
        public Task<AllOrdersQueryResult> Handle(AllOrdersQuery request, CancellationToken cancellationToken)
        {
            return Task.FromResult(new AllOrdersQueryResult() { 
                OrderDTOs = new OrderDTO[] { 
                    new OrderDTO() { Id = 1, Customer = "MyCustomerName" }
                } 
            });
        }
    }
}

Use the CommandDispatcher for dispatch commands to the right handler

OrderController.cs
using QuokkaDev.Cqrs;
using Microsoft.AspNetCore.Mvc;

namespace QuokkaDevCqrsDemoApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly IQueryDispatcher queryDispatcher;

        public OrderController(IQueryDispatcher commandDispatcher)
        {
            this.queryDispatcher = commandDispatcher;
        }

        [HttpGet]
        public async Task<IActionResult> AllOrders()
        {
            AllOrdersQueryResult result = await queryDispatcher.Dispatch<AllOrdersQuery, AllOrdersQueryResult>(new AllOrdersQuery());
            return Ok(result);
        }
    }
}
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 (1)

Showing the top 1 NuGet packages that depend on QuokkaDev.Cqrs:

Package Downloads
QuokkaDev.Cqrs.Decorators

Contains some decorators to enable cross cutting concerns on command and query dispatchers

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 525 6/8/2022
3.1.0-alpha1 197 6/11/2022
3.1.0-alpha0 180 6/9/2022
3.0.3 461 6/8/2022
3.0.2 455 6/8/2022
3.0.1 470 6/1/2022
3.0.0 953 5/30/2022
3.0.0-alpha5 204 6/2/2022