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.