django-cqrs
django-cqrs copied to clipboard
Events triggering actions
Hi thanks for the example implementation, I'm enjoying looking at it.
In event_handler.py I noticed that the event is triggering the corresponding action; for example a post_created_event
triggers the creation of the post.
My understanding was that the event should represent something that has happened already? My plan for implementing something similar was to perform the action first and then save the corresponding event.
For example my action might be CreatePost
and once the post is created I then store a PostCreatedEvent
.
What do you think of this? Would be good to get your views!
Many thanks, Dom
@DomHudson If we create the post first, and then create the event, this might get us to an inconsistent state.
In the implementation I've done
- Event is created first
- Actual data is created after the event.
So if there is an issue creating the data, I can replay the events to get the data to a proper consistent state.
But if I create the object first and then the event, if the event creation fails then there is a chance of inconsistency, as replaying the events would miss out this data (Since storing this event failed).
Think of this as how postgres uses WAL https://en.wikipedia.org/wiki/Write-ahead_logging
What if you want to reprocess the events to restore the state on a certain date and time or create different view models?