Event Sourcing CQRS and request events ending at Requested

by Waku-2   Last Updated August 28, 2018 04:05 AM

In ES/CQRS, do/can we treat requests as events?

Consider the following flow

A guest visited some url and posted data to create a new account. Here is one way to implement it

1- Invoke CreateNewUserCommand [payload = posted data]

2- Invoke CreateUserHandler(CreateNewUserCommand)

3- Fire CreateNewUserRequestedEvent [payload= posted data]

4a- Handle it in CreateNewUserEventHandler

4b- Validate the input against invariants 

5- If validation failed [invalid email, duplicate user, payment not sufficient etc] fire another event
UserCreationFaile:PaymentInsufficent [paylod= posted data]
UserCreationFaile:EmailInvalid [paylod= posted data]
UserCreationFaile:UserAlreadyExists [paylod= posted data]

6- If validation is successfull
fire an event
NewUserCtreated [payload= posted data + created user id]

Martin Fowler clearly states that we persist domain events when state is changed. Now if we consider CreateNewUserRequestedEvent, it is just an intention to change the state.

Later on when we replay the events, CreateNewUserRequestedEvent does not count as an event to take part in state update but NewUserCtreated event does.

However, if we look at the system externally, a request to do something [CreateNewUserRequestedEvent] is also an event. Take the example of a use case when we made the request, request got saved, but server goes down that instant.

Next time the server goes up, we can pick up from where we left and continue creating the user.

Related Questions

How to create new aggregate root in CQRS?

Updated February 21, 2017 13:05 PM

CQRS - Passing aggregate root as argument

Updated February 25, 2017 23:05 PM

Advantages of Event Sourcing over history tables

Updated July 21, 2015 13:02 PM