- Opening a new account with an owner’s name.
- Depositing money to increase the account’s balance.
- Withdrawing money to decrease the account’s balance (ensuring the account does not underflow).
Defining Commands
A Command represents a user’s intent or instruction to change state. Because commands represent an action that should happen, they are always named using the present tense (e.g.,OpenAccount, DepositMoney).
In Rust, we represent commands as a simple enum. This enum encapsulates all possible actions that can be executed on our bank account, along with any parameters required for those actions.
Create your command enum like so:
Command Design Guidelines
When designing commands in your own applications, keep these best practices in mind:- Be Specific: Make commands highly specific to the business operation. Prefer
DepositMoneyoverUpdateBalance { delta: i64 }. - Include Relevant Payload: Provide only the data required to validate and execute the operation.
- No Aggregate ID in Payload (Optional): In our framework, the aggregate ID is loaded and specified at the routing level (e.g., when calling
repo.execute(&id, command, ...)). While some commands (likeOpenAccount) might include the initial ID in their payload for initialization, subsequent commands (likeDepositMoney) only need the operational parameters likeamount.