CQR 1.0.1
dotnet add package CQR --version 1.0.1
NuGet\Install-Package CQR -Version 1.0.1
<PackageReference Include="CQR" Version="1.0.1" />
<PackageVersion Include="CQR" Version="1.0.1" />
<PackageReference Include="CQR" />
paket add CQR --version 1.0.1
#r "nuget: CQR, 1.0.1"
#:package CQR@1.0.1
#addin nuget:?package=CQR&version=1.0.1
#tool nuget:?package=CQR&version=1.0.1
CQR - A Simple CQRS Library for .NET Core
CQR is a lightweight, open-source library that provides a simple alternative to MediatR for implementing CQRS (Command-Query Responsibility Segregation) in .NET applications.
It focuses exclusively on handling commands and queries with asynchronous support, allowing developers to decouple request handling logic from the calling code.
Note: This library is intentionally minimal and does not include advanced MediatR features such as notifications, pipelines, or behaviors. It is designed purely for CQRS.
Features
- Send commands and queries with or without a response.
- Automatic handler resolution with dependency injection support.
- Simple, easy-to-understand API.
- Supports both
IRequest<TResponse>
andIRequest
(fire-and-forget) patterns. - Internal caching for efficient handler lookup.
Installation
You can install CQR via NuGet:
dotnet add package CQR --version 1.0.0
Or using the NuGet Package Manager:
Install-Package CQR -Version 1.0.0
Getting Started (Example)
Step 01:
Add CQR to DI Services
builder.Services.AddCQR(config =>
{
config.AddAssembly(typeof(Program).Assembly);
...
});
You can add assemblies as many as you want.
Step 02:
Create Commands and Queries
A command class:
using CQR.Abstractions;
namespace Application.Commands;
public class CreateTaskCommand : IRequest
{
public string AssignedUser { get; set; }
public string Name {get;set;}
public string Description {get;set;}
}
In here, CreateTaskCommand
implements IRequest
from the CQR.Abstractions
package.
A query class:
using CQR.Abstractions;
using Domain.Models;
namespace Application.Queries.GetAllTasksQuery;
public class GetAllTaskQuery : IRequest<TaskModel[]>
{
}
In here, GetAllTaskQuery
implements IRequest<TaskModel[]>
, specifying the expected result type.
Step 03
Create command and query handlers
Handlers are responsible for processing commands and queries. Each command or query class must have a corresponding handler that contains the business logic for that operation.
A command handler:
using CQR.Abstractions;
using Infrastructure.Repository;
namespace Application.Commands;
public class CreateTaskCommandHandler(ITaskRepository repository) : IRequestHandler<CreateTaskCommand>
{
public async Task HandleRequestAsync(CreateTaskCommand request)
{
await repository.AddTask(request);
}
}
A query handler:
using CQR.Abstractions;
using Domain.Models;
using Infrastructure.Repository;
namespace Application.Queries.GetAllTasksQuery;
public class GetAllTaskQueryHandler : IRequestHandler<GetAllTaskQuery, TaskModel[]>
{
private readonly ITaskRepository _repository;
public GetAllTaskQueryHandler(ITaskRepository repository)
{
_repository = repository;
}
public async Task<TaskModel[]> HandleRequestAsync(GetAllTaskQuery request)
{
var res = await _repository.GetAllTasks();
return res.ToArray();
}
}
Step 04
In this final step, we integrate the CQRS-based commands and queries into an actual API controller using the ICqr
abstraction. This provides a clean and centralized way to dispatch commands and queries without tightly coupling the controller to specific implementations.
using Application.Commands;
using Application.Commands.DeleteTaskCommand;
using Application.Queries.GetAllTasksQuery;
using Application.Queries.GetTaskByUserQuery;
using CQR.Abstractions;
using Microsoft.AspNetCore.Mvc;
namespace TaskFlow.CQRS.Controllers;
[ApiController]
[Route("api/[controller]")]
public class TaskController: ControllerBase
{
private readonly ICqr _cqr;
public TaskController(ICqr cqr)
{
_cqr = cqr;
}
[HttpPost]
public async Task<IActionResult> CreateTask([FromBody] CreateTaskCommand request)
{
await _cqr.SendRequest(request);
return Ok("Created a Task");
}
[HttpGet]
public async Task<IActionResult> GetTasks()
{
var result = await _cqr.SendRequest(new GetAllTaskQuery());
return Ok(result);
}
}
✅ With this step, your CQRS-based API is now fully functional — cleanly separated into commands, queries, handlers, and endpoints.
Questions or Feedback?
If you have any questions or feedback about this implementation or the CQRS pattern in general, don't hesitate to reach out or open a discussion.
Thanks for checking out this project! 🎉
Happy coding! 🚀
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.8)
- System.Linq (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release of the CQR library.