Lab 1 - TDD Aggregate Root - Create Kitchen Commands with Business Logic and Invariants
1. Summary
The first responsibility of an Aggregate is to maintain the business invariants of the domain concept it represents, and to ensure that these invariants are transactionally consistent.
In this lab, we’ll implement these concerns by treating the Aggregate as a state machine. It will ensure that Aggregates only transition state when the appropriate conditions are met.
We’ll complete 18 stories:
-
When a
KitchenOrderis created, its status isNEW. -
When a cook starts preparing a
KitchenOrder, its status becomesPREPPING. -
Only a
NEWKitchenOrdercan begin prepping. -
When a cook starts baking a
KitchenOrder, its status becomesBAKING. -
Only a
PREPPINGKitchenOrdercan begin baking. -
When an assembler starts assembling a
KitchenOrder, its status becomesASSEMBLING. -
Only an
BAKINGKitchenOrdercan begin assembly. -
When an assembler finishes assembling a
KitchenOrder, its status becomesASSEMBLED. -
Only an
ASSEMBLINGKitchenOrdercan finish assembly. -
When a
Pizzais created, its status isNEW. -
When a cook starts preparing a
Pizza, its status becomesPREPPING. -
Only a
NEWPizzacan begin prepping. -
When a cook finishes preparing a
Pizza, its status becomesPREPPED. -
Only a
PREPPINGPizzacan finish prepping. -
When a cook starts baking a
Pizza, its status becomesBAKING. -
Only a
PREPPEDPizzacan begin baking. -
When a cook finishes baking a
Pizza, its status becomesBAKED. -
Only a
BAKINGPizzacan finish baking.
You can run com.mattstine.dddworkshop.pizzashop.suites.Lab1Suite to check your progress in this lab.
2. Instructions
-
KitchenOrderandPizzawill be responsible for maintaining business invariants. -
Each command that an actor can execute on an Aggregate exists as a method on that Aggregate. The command method is responsible for maintaining the business rules and executing the state transitions.
-
For example, when a cook starts baking a pizza, the
Pizza.startBake()method is called. -
Implement each of these command methods on the two Aggregates.
-
Each state of an Aggregate has a status method that explicitly exposes the state of the state machine, but hides the implementation.
-
For example, to check if a pizza is baking, the
Pizza.isBaking()method is called. -
Implement each of these status methods on the two Aggregates.
-
If you’re looking for hints on how to implement these command and status methods, take a look at the Aggregates in the Ordering or Payments contexts.