QuokkaDev.Cqrs.Abstractions
3.1.0-alpha0
See the version list below for details.
dotnet add package QuokkaDev.Cqrs.Abstractions --version 3.1.0-alpha0
NuGet\Install-Package QuokkaDev.Cqrs.Abstractions -Version 3.1.0-alpha0
<PackageReference Include="QuokkaDev.Cqrs.Abstractions" Version="3.1.0-alpha0" />
paket add QuokkaDev.Cqrs.Abstractions --version 3.1.0-alpha0
#r "nuget: QuokkaDev.Cqrs.Abstractions, 3.1.0-alpha0"
// Install QuokkaDev.Cqrs.Abstractions as a Cake Addin #addin nuget:?package=QuokkaDev.Cqrs.Abstractions&version=3.1.0-alpha0&prerelease // Install QuokkaDev.Cqrs.Abstractions as a Cake Tool #tool nuget:?package=QuokkaDev.Cqrs.Abstractions&version=3.1.0-alpha0&prerelease
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 | 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
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on QuokkaDev.Cqrs.Abstractions:
Package | Downloads |
---|---|
QuokkaDev.Cqrs
A package for apply CQRS pattern in .NET projects |
|
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 | 625 | 6/8/2022 |
3.1.0-alpha1 | 209 | 6/11/2022 |
3.1.0-alpha0 | 204 | 6/9/2022 |
3.0.3 | 552 | 6/8/2022 |
3.0.2 | 568 | 6/8/2022 |
3.0.1 | 574 | 6/1/2022 |
3.0.0 | 1,048 | 5/30/2022 |
3.0.0-alpha5 | 207 | 6/2/2022 |