0Pricing
C# Academy · Lesson

CQRS Concepts

Split commands from queries for clarity.

CQRS Concepts is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is CQRS?

CQRS stands for Command Query Responsibility Segregation. The idea is simple: separate the code that changes state (commands) from the code that reads state (queries).

// Commands : change state, return little or nothing
// Queries  : return data, change nothing

Commands

A command expresses an intent to change the system - create an order, cancel a subscription. It is named imperatively and typically returns only an id or nothing.

public record CreateOrderCommand(int CustomerId, string[] Items);
public record CancelOrderCommand(int OrderId);

Queries

A query asks for data without side effects. It is named for what it returns and carries any filter parameters.

public record GetOrderByIdQuery(int OrderId);
public record ListOrdersQuery(int CustomerId, int Page);

Why Separate Them?

Reads and writes have different needs:

  • Reads want to be fast, denormalized and cache-friendly.
  • Writes want validation, business rules and consistency.

Separating them lets each side evolve and scale independently.

// Read side  -> optimized projections / DTOs
// Write side -> domain rules / transactions

The Handler Pattern

Each command or query is processed by a dedicated handler. One message, one handler, one responsibility - which keeps classes small and focused.

// CreateOrderCommand  -> CreateOrderHandler
// GetOrderByIdQuery    -> GetOrderByIdHandler

Thin Controllers

With CQRS, controllers become thin. They build a message and dispatch it; all logic lives in the handler. This removes fat controllers and improves testability.

[HttpPost]
public async Task<IActionResult> Create(CreateOrderCommand cmd)
{
    var id = await _sender.Send(cmd);
    return CreatedAtAction(nameof(Get), new { id }, null);
}

CQRS Is Not Event Sourcing

A common confusion: CQRS is just the read/write separation. Event sourcing (storing state as a log of events) is a separate pattern that pairs well with CQRS but is not required.

// You can do CQRS with a plain single database.

Single vs Separate Data Stores

Basic CQRS uses one database for both sides. Advanced setups use separate read and write stores kept in sync, accepting eventual consistency for scale. Start simple.

// Level 1: one DB, separate handlers (most apps)
// Level 2: separate read/write models
// Level 3: separate read/write databases

Benefits

CQRS gives you:

  • Clear, single-purpose handlers.
  • Easier testing - each handler tested in isolation.
  • A natural place for cross-cutting concerns (validation, logging).
  • Independent scaling of reads and writes.
// One class per use case = easy to find and test

Costs

CQRS adds more classes and indirection. For a simple CRUD app it can be overkill. Apply it where business logic is rich or the domain is complex.

// Trade-off: clarity & scale vs. boilerplate

Enter MediatR

The MediatR library implements the in-process mediator pattern that dispatches each message to its handler, so you do not wire them up by hand. The rest of this course uses it.

dotnet add package MediatR

Quick Check

Confirm the core distinction.

Recap

You learned CQRS concepts:

  • Separate commands (state changes) from queries (reads).
  • Each message has one focused handler, keeping controllers thin.
  • CQRS is independent of event sourcing and can use a single database.
  • MediatR dispatches messages to handlers.

Next: commands and handlers with MediatR.

Frequently asked questions

Is the “CQRS Concepts” lesson free?

Yes — the full text of “CQRS Concepts” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “CQRS Concepts”?

Split commands from queries for clarity. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “CQRS Concepts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. CQRS Concepts
  2. Commands and Handlers with MediatR
  3. Pipeline Behaviors
  4. Notifications and Events
← Back to C# Academy