Lab 4 - TDD Aggregate Repository - Rehydrate Kitchen Aggregates by Reference

1. Summary

When using Event Sourcing without CQRS, the only way to find an Aggregate is to "rehydrate" it from the list of events that it has published. This find operation can only be indexed by primary key.

In this lab, we’ll implement find by reference for the Kitchen context Aggregates.

We will complete two stories:

  1. We will support finding a KitchenOrder by its KitchenOrderRef.

  2. We will support finding a Pizza by its PizzaRef.

You can run com.mattstine.dddworkshop.pizzashop.suites.Lab4Suite to check your progress in this lab.

2. Instructions

  1. Our generic InProcessEventSourcedRepository implementation depends primarily on two methods that must exist on each Aggregate:

    • identity

    • accumulatorFunction

  2. The implementations of both of these methods on KitchenOrder and Pizza are broken. You have to fix them.

  3. Implementing identity is fairly straightforward. Use the builder for the Aggregate to construct a new instance, setting each property to its IDENTITY value, and return it.

  4. The implementation of accumulatorFunction is fine, but you’ll need to fix the Accumulator object it returns by implementing its apply method.

  5. To properly implement apply, you need to check the type of the AggregateEvent passed as an argument, and for each event type published for that Aggregate, perform the appropriate state transformation.

  6. If you’re looking for hints, try looking at the apply implementations in the Ordering and Payments contexts.