I'm developing an event-sourced/CQRS application and do not understand the best strategy to use when building the read model projections when additional domain data is needed. A partial representation of the write model aggregates and events are as follows ...
I'm developing a read model which looks as follows ...
I'm running into an issue when trying to represent the warehouseName in the WorkOrderToBeReceivedModel. When the WorkOrderCreatedEvent is thrown, the projector picks up this event but only has access to the warehouseId and not warehouseName. I see three approaches for solving this problem, however, I’m not sure which one is the best …
- Modify the WorkOrderCreatedEvent and include warehouseName in addition to warehouseId. I will then have access to the name in my projection and can persist it at the time of creation. This would work, however, as soon a warehouse name was updated, the data would become stale. I can listen to the WarehouseNameUpdatedEvent in this projection, however, this would become cumbersome when adding additional fields for related aggregates (ie CreatorId, ProductId, etc.). The upside to this approach is that I am not adding dependencies on other projections or repositories from the write side.
- Inject the aggregate write model warehouseRepository into the projector, and then use this repository to reconstruct the warehouse from the event store and access the name. I’ve now added a dependency on the write model. I’m not sure if this is an anti-pattern in the ES/CQRS world. In addition, the data will still become stale the same way as mentioned for 1.
- Build a warehouse read model projection and then join to this table from the work order projection. I’m making things a bit fragile by depending on another projection and might run into issues if the warehouse projection is not “in-step” with the work order projection.
Any advice would be appreciated.