SOLID & Design Patterns Interview Question #64

What is the difference between a command and a query in CQRS?

Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate

Quick Interview Answer

A command requests a state-changing operation and normally expresses business intent, such as ApproveOrder or CancelBooking. A query requests information and should not change observable business state.

Detailed Explanation

A command requests a state-changing operation and normally expresses business intent, such as ApproveOrder or CancelBooking. A query requests information and should not change observable business state.

Keeping these responsibilities distinct can make validation, authorization, performance optimization, and models easier to reason about.

Code Example

public record ApproveOrderCommand(int OrderId);

public record GetOrderDetailsQuery(int OrderId);

// Command -> changes state
// Query   -> returns data