I’m building a system using CQRS pattern with the write model depending on EventStore DB, I have an aggregate root which contains three sub entities, each entity can be updated separately, e.g I can do the following commands separately (Create, UpdateA, UpdateB, UpdateC).
Of course after each command I trigger an event for the EventStore and the same for the read model to update its model props.
I need to add “Copy” command, so I thought to clone the aggregate root and trigger an event for each part (entity), so the copy will be as the following:
- Clone the whole root with the sub entities.
- Trigger Create event.
- Trigger AUpdated event.
- Trigger BUpdated event.
- Trigger CUpdated event.
I thought this is better and more consistent than triggering one event contains ALL the root with its sub entities.
Now the issue is, some times I receive AUpdate, B, C before the Create event, in the read model, so will not be able to execute the updates because the create never happened and there’s no element with the Id comes with the “updated” events.
What is a good solution for this case or similar cases where I can’t guarantee the order of the events?
Is this a “Saga” ?
Is it a good solution to check (when I receive the Updated items) if the item exists I continue the update, if not, I create an empty one with only the Id and continue the update?