Assume that Book and Author are Aggregate Roots in my model.
In read model i have table AuthorsAndBooks which is a list of Authors and Books joined by Book.AuthorId
When BookAdded event is fired i want to receive Author data to create a new AuthorsAndBooks line.
Because Book is an Aggregate Root, information about Author doesn’t included in BookAdded event. And i cannot include it because Author root doesn’t have getters (according to guidelines of all examples and posts about CQRS and Event Sourcing).
Usually i receive two types of answers on this question:
- Enrich your domain event with all data you need in event handlers. But as i said i cannot do it for Aggregates Roots.
- Use available data from View Model. I.e. load
Authorfrom View Model and use it to buildAuthorsAndBooksrow.
The last one has some problems with concurrency. Author data can be not available in View Model at the time BookAdded event is handling.
What approach do you use to solve this? Thank you.
As a general advice, let the event handlers be idempotent and make sure you can deal with out of order message handling (either by re-queuing or building in mechanisms to fill in missing data).
On the other hand, do question why author and book are such desperate aggregate roots. Maybe you should copy from the author upon adding a book (what the f* is “adding a book”, how’s that a command). The problem is all these made-up examples. Descend to the real world, I doubt your problem exists.