Inspired by Peter C Marks' ddd_elixir_stage1_umbrella repo.
I've implemented the cargo event handling workflow using the approach dictated by the Commanded CQRS/ES library for Elixir.
The flow is as follows:
HandlingEventController
web controller constructs aTrackHandling
command struct from given POST params.TrackHandling
command is dispatched using theCommandRouter
, which has configured the command to handler/aggregate mapping.TrackHandling
command is routed to theCargoTracking
module and a GenServer is started/resumed to host theCargo
aggregate.- The
CargoTracking
module returns aHandlingEvent
domain event, which is appended to the events for theCargo
aggregate identified bycargo_id
from the command. - The
Cargo.apply/2
function is called to mutate the cargo state, appending the handling event to its list of handling events. This function is also used when rebuilding the cargo state by replaying its events.
TrackHandling
=>CommandRouter
=>CargoTracking
=>HandlingEvent
=>Cargo
Commanded provides the means to host each aggregate (e.g. Cargo
) within its own Elixir process. It is responsible for rehydrating the aggregate from its persisted events, and appending new events to storage. You can configure event handlers in Commanded, such as to project events into a denormalised read model. The read model is queried for data to be returned to the end user.
Take a look at Conduit if you would like to see an entire Elixir application built using Commanded following the CQRS/ES pattern.