Request for example with multiple validated events

51 views
Skip to first unread message

Danil Suits

unread,
May 12, 2016, 3:33:02 PM5/12/16
to DDD/CQRS

Can anybody share a simple example of a command that generates multiple events, where the validation requires checks on the intermediate state(s)?

I feel like there might be something in trading/ledger book, but I don't quite see what it is.

Riffing on one of Greg's old examples[1], the OrderBook (?)

placeOrder(args) {
   
State initialState = this.state

   
Event orderPlaced = orderPlaced(initialState, args)

   
State currentState = initialState.apply(orderPlaced)  

   
while (currentState.hasTrades()) {

       
// Can this fail???
       
Event nextTrade = trade(currentState)

        currentState
= currentState.apply(nextTrade)
   
}

   
// Does placeOrder always end with OrderCancelled?  I don't know this domain.
   
Event orderCancelled = orderCancelled(currentState, args)

   
State finalState = currentState.apply(orderCancelled)

   
//...
}

Based on my current understanding, State.apply() should never fail -- that's the left fold we will be using later to rehydrate state from the event history.  Therefore, if any inconsistencies are going to be detected, that needs to happen when the events are being generated.

Based on my limited understanding, that trade method can't fail -- either there are orders to pair off, or there aren't; there isn't an scenario where pairing off matching orders would leave the OrderBook in an invalid state.  If that's right, then the above sample isn't really all that different from

    List<Event> trades = currentState.trades()

    for
(Event nextTrade : trades) {
        currentState
= currentState.apply(nextTrade)
   
}

Which is to say that the construction of the trade events doesn't really depend on the intermediate states (?); you don't need to visit the intermediate states to generate a correct history.

Does anybody have an example of a command where the intermediate states are necessary to protect the invariant?  Or a simple way to explain how intermediate validation would apply in this example?

Thanks,
Danil

[1] https://groups.google.com/forum/#!searchin/dddcqrs/multiple$20events/dddcqrs/ZyiQ-LlQQWs/V75wtDfcm8EJ


Reply all
Reply to author
Forward
0 new messages