Hi,
I am still somewhat new to the CQRS/ES patterns and the Axon framework. I can see that the Axon framework gives us a great deal of support in terms of implementing these patterns within the context of Java Spring based REST services.
Various articles and postings I have come across seem to suggest that one of the advantages of using CQRS/ES is that it is easy to step through the history of an entity by re-applying events one by one to that entity. This lead me to think that this ability to step through an entity’s history may be reflected by specific constructs within the Axon framework. However, I don’t think I found such constructs.
In exploring how we might go about providing the ability to step through the history of (for example) a case, I ended up doing it like this:
- REST endpoint creates a Query object that includes an event sequence number and aggregate ID amongst its query parameters
- REST endpoint sends the Query to the query gateway and waits for the case to be returned to it, at which point it returns the case to ts client (as JSON)
- @QueryHandler annotated method on a Handler class calls readEvents(aggregate ID) on the EventStore to obtain the stream of event messages that have occurred so far for the case identified
- @QueryHandler method iterates through the event messages read until it reaches the event with the sequence number specified and directly applies the event payload of each such message to a Case object read model view of the case that is built up in memory
- Once the iteration is complete, the @QueryHandler method returns the Case object so built to its client
- The Case object is returned back through the call chain ultimately to the REST client
REST Query Gateway Handler EventStore Case
create | | | | |
--------->| | | | |send | | | |
----------------------->| | | |invoke | | |
-------------------------------------->| | |create |
---------------------------->|events = | |
readEvents | |
----------->| |for | |
each | |
event | |applyEvent |
---------------------------->|
Case | | | | |
<--------------------------------------| | |
With hindsight, this solution does not seem that complex, but I wonder whether I may have missed anything in Axon that could make it even simpler?
Thanks,
Vaughan Jackson.