pub trait Aggregate {
type State: Default;
type Command;
type Event;
type Error: Error;
const NAME: &'static str;
// Required methods
fn handle_command(
state: &Self::State,
command: Self::Command
) -> Result<Vec<Self::Event>, Self::Error>;
fn apply_event(state: Self::State, payload: Self::Event) -> Self::State;
}
Expand description
The Aggregate trait is responsible for validating commands, mapping commands to events, and applying events onto the state.
An Aggregate should be able to derive its own state from nothing but its initial configuration, and its event stream. Applying the same events, in the same order, to the same aggregate, should always yield an identical aggregate state.
This trait is purposefully synchronous. If you are implementing this trait, your aggregate should not have any side effects. If you need additional information to handle commands correctly, then consider looking up that information and placing it in the command.
Required Associated Types§
sourcetype State: Default
type State: Default
Internal aggregate state. This will be wrapped in crate::state::AggregateState
and could
be used to validate commands.
sourcetype Command
type Command
A command is an action that the caller can execute over an aggregate in order to let it emit an event.
Required Associated Constants§
sourceconst NAME: &'static str
const NAME: &'static str
The NAME
const is responsible for naming an aggregate type.
Each aggregate type should have a name that is unique among all the aggregate types in your application.
Aggregates are linked to their instances & events using their NAME
and their aggregate_id
.
Be very careful when changing NAME
, as doing so will break the link between all the aggregates
of their type, and their events!
Required Methods§
sourcefn handle_command(
state: &Self::State,
command: Self::Command
) -> Result<Vec<Self::Event>, Self::Error>
fn handle_command( state: &Self::State, command: Self::Command ) -> Result<Vec<Self::Event>, Self::Error>
Handles, validate a command and emits events.
§Errors
Will return Err
if the user of this library set up command validations. Every error here
could be just a “domain error”. No technical errors.
sourcefn apply_event(state: Self::State, payload: Self::Event) -> Self::State
fn apply_event(state: Self::State, payload: Self::Event) -> Self::State
Updates the aggregate state using the new event. This assumes that the event can be correctly applied to the state.
If this is not the case, this function is allowed to panic.