Event upgrade
At some point, you might find the need to replace an event with zero or more events. Some use cases might be:
- A previous application version introduced a domain error in the form of a wrong event being emitted from the aggregate.
- The domain has changed, either from a change in requirements or simply from a better understanding of the domain.
EventFlow event upgraders are invoked whenever the event stream is loaded from the event store. Each event upgrader receives the entire event stream one event at a time.
A new instance of an event upgrader is created each time an aggregate is loaded. This enables you to store information from previous events on the upgrader instance to be used later, e.g., to determine an action to take on an event or to provide additional information for a new event.
Note that the ordering of event upgraders is important as you might implement two upgraders, one to upgrade an event from V1 to V2 and then another upgrading V2 to V3. EventFlow orders the event upgraders by name before starting the event upgrade.
Warning
Be careful when working with event upgraders that return zero or more than one event, as this has an influence on the aggregate version and you need to make sure that the aggregate sequence number on upgraded events is valid in regard to the aggregate history.
Example - removing a damaged event
To remove an event, simply check and only return the event if it's not the event you want to remove.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Example - replace event
To upgrade one event to another, you should use the IDomainEventFactory.Upgrade
to help migrate metadata and create the new event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|