django-cqrs icon indicating copy to clipboard operation
django-cqrs copied to clipboard

Events triggering actions

Open DomHudson opened this issue 6 years ago • 2 comments

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 avatar Nov 09 '18 16:11 DomHudson

@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

  1. Event is created first
  2. 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

Dineshs91 avatar Nov 10 '18 10:11 Dineshs91

What if you want to reprocess the events to restore the state on a certain date and time or create different view models?

gpietro avatar Sep 24 '19 12:09 gpietro