What is a Write Model?
In Domain-Driven Design (DDD), the Write Model is responsible for handling commands and applying changes to the system's state. It focuses exclusively on the processes involved in creating, updating, or deleting domain entities and aggregates.
Key Characteristics:
Command-Driven: The Write Model processes commands, which represent user intents or system instructions.
Business Logic: It encapsulates the domain rules and ensures that all changes to the state comply with business constraints.
Consistency: Modifications made through the Write Model are consistent with the domain model and its invariants.
Relation to CQRS:
In a CQRS (Command Query Responsibility Segregation) architecture, the Write Model is distinct from the Read Model. While the Write Model is optimized for processing changes, the Read Model is tailored for querying data. This separation improves scalability and maintainability.
Example:
Imagine an e-commerce system where customers place orders. The Write Model would validate and process commands like PlaceOrder
or CancelOrder
, ensuring the domain rules (e.g., inventory availability) are enforced.