How would one apply command query separation (CQS), when result data is needed from a command?

后端 未结 10 1996
慢半拍i
慢半拍i 2021-01-30 06:25

In wikipedia\'s definition of command query separation, it is stated that

More formally, methods should return a value only if they are referentially t

10条回答
  •  情话喂你
    2021-01-30 06:57

    Well, this is a pretty old question but I post this just for the record. Whenever you use an event, you can instead use a delegate. Use events if you have many interested parties, otherwise use a delegate in a callback style:

    void CreateNewOrder(Customer buyer, Product item, Action onOrderCreated)
    

    you can also have a block for the case where the operation failed

    void CreateNewOrder(Customer buyer, Product item, Action onOrderCreated, Action onOrderCreationFailed)
    

    This decrease the cyclomatic complexity on the client code

    CreateNewOrder(buyer: new Person(), item: new Product(), 
                  onOrderCreated: order=> {...},
                  onOrderCreationFailed: error => {...});
    

    Hope this helps any lost soul out there...

提交回复
热议问题