CQR 1.0.1

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

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> and IRequest (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 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
1.0.1 43 8/22/2025
1.0.0 48 8/22/2025

Initial release of the CQR library.