QuokkaDev.Cqrs
3.1.0-alpha1
See the version list below for details.
dotnet add package QuokkaDev.Cqrs --version 3.1.0-alpha1
NuGet\Install-Package QuokkaDev.Cqrs -Version 3.1.0-alpha1
<PackageReference Include="QuokkaDev.Cqrs" Version="3.1.0-alpha1" />
paket add QuokkaDev.Cqrs --version 3.1.0-alpha1
#r "nuget: QuokkaDev.Cqrs, 3.1.0-alpha1"
// Install QuokkaDev.Cqrs as a Cake Addin #addin nuget:?package=QuokkaDev.Cqrs&version=3.1.0-alpha1&prerelease // Install QuokkaDev.Cqrs as a Cake Tool #tool nuget:?package=QuokkaDev.Cqrs&version=3.1.0-alpha1&prerelease
QuokkaDev.CQRS
A package for apply CQRS pattern in .NET projects and add cross cutting concerns
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.Cqrs and 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 | Versions 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. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.1)
- QuokkaDev.Cqrs.Abstractions (>= 3.1.0-alpha1)
- Scrutor (>= 4.1.0)
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 |