user449689
user449689

Reputation: 3154

Event sourcing: correct approach to ignore events

I'm currently working on a project where event sourcing is used. This is my first experience with ES therefore sometimes I don't know if the approach I use to work out some problems is correct or not.

Consider this scenario: I have a task based UI where each action produces a command, that creates one or more events.

At a certain point the user can start a process, say that an event called ProcessStarted is generated. From there he will be able to make changes to the aggregate, therefore producing other events.

He can go on, or, if he wants, he can revert the state of the aggregate at the last event before he started the above-mentioned process, say that an event called ProcessReverted is generated.

If the process is reverted, more operations can be made on the aggregate, but the events that concurred in the process from the ProcessStarted to the ProcessReverted events must not be considered.

Therefore, when the aggregate is re-hydrated I want:

What's the best approach to reach my goal? I cannot think of a solution that would be elegant and fitting in an ES context.

I'm not posting any code here because I'm not looking for implementation details, but I'm looking for a strategy, I hope to get some advices from people with more experience than me and I hope that this would help me better understand how to leverage the power and flexibility of ES.

Upvotes: 1

Views: 278

Answers (1)

Constantin Galbenu
Constantin Galbenu

Reputation: 17683

You should do this inside your aggregate, by storing a snapshot of the state every time a ProcessStarted event is applied; when the ProcessReverted is applied then the entire state is replaced by the saved one in the snapshot. This is simple because the events are always applied in the order they were emitted.

So, there is no need for an external component or infrastructure service, it's just internal aggregate logic.

Upvotes: 2

Related Questions