top of page

What is a Command?

In Domain-Driven Design (DDD), a Command represents an intention to change the state of the system. It is an instruction or a request to perform a specific action or operation within a domain. Commands are typically issued by users or other systems and are handled by the application's core business logic.


Key characteristics of a Command in DDD:

Intent: A Command clearly defines the intent to perform an action, such as "place an order," "approve a transaction," or "change a customer's address."


Immutable: Once a Command is created, it should not change. It captures all necessary information (parameters) for the action when it's created.


No Side Effects: A Command itself doesn't modify the state of the system; instead, it's sent to a handler or an application service that applies the change.


Commands vs. Queries: In DDD, Commands are often contrasted with Queries. While Commands intend to change the state of the system, Queries are used to retrieve data without modifying the state.


Part of CQRS (Command Query Responsibility Segregation): Commands often play a role in CQRS architecture, where Commands handle write operations (changing state), while Queries handle read operations (fetching data).


Example of Command:

Imagine an e-commerce system. If a user wants to place an order, the system might issue a PlaceOrderCommand. This command would contain all the necessary details (e.g., product IDs, quantities, customer information) and would be handled by an application service or domain logic that processes the order.


In summary, a Command in DDD is used to encapsulate an action or intention to modify the system, ensuring a clear and well-defined approach to handling state changes.

bottom of page