Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CQRS, how do I communicate a list of allowable methods on an aggrregate to the UI?

I have an aggregate root 'Order', and it has a number of methods on it which internally set its 'Status' field:

  • Submit
  • Place On Hold
  • Take Off Hold
  • Confirm
  • Cancel
  • etc.

The available actions are dependent upon the current Status of the order (e.g. it can't be put on hold if it's already on hold). The problem is that I need to provide a query to tell the UI which commands are are available so I can hide the operations that would otherwise throw an InvalidOperationException.

How do I do this with minimal DRY violation?

like image 935
Josh Kodroff Avatar asked Oct 11 '22 02:10

Josh Kodroff


1 Answers

The simplest solution is to project current status along with available transitions to read/query model and select it along with the data to display.

Example: PlaceOnHoldCommand results in OrderPlacedOnHoldEvent which (besides being put in EventStore) is published and handled by OrderTransitionsEventHandler which denormalizes available transitions to a DB table associated with Order. The client selects available transitions and acts accordingly (hides unavailable buttons or sth. like that).

This is of course one of options. Don't expect however that there will be no duplication whatsoever. CQRS helps to manage complexity and sometimes this means slight violations of DRY occur.

like image 150
kstaruch Avatar answered Oct 18 '22 09:10

kstaruch