- A customer cannot withdraw
$150if their account only has$100. - A customer cannot deposit a negative amount or
$0. - A customer cannot deposit money into an account that has not been opened yet.
Defining Domain Errors
Rather than using generic error strings or general database codes, we define our domain errors as a simple Rust enum. This makes errors highly typed, explicit, and easy to handle or translate in our application edge (such as web routers or GraphQL layers).What About Domain Services?
In some applications, validating a command requires interacting with an external service or performing a query that spans multiple aggregates.- In Domain-Driven Design, when an operation doesn’t naturally belong to a single Aggregate Root, we encapsulate that logic inside a Domain Service.
- In our framework, keeping command handling pure and synchronous is a top priority to maintain local reasoning and easy testability.
- If a validation requires external data, we recommend querying that data in your application layer first, and then passing the results directly into the command payload or as a parameter to the command execution, keeping the aggregate’s
handlemethod completely free of network or database connections.