Trait esrs::aggregate::Aggregate [−][src]
pub trait Aggregate {
type State: Default + Clone + Debug + Send + Sync;
type Command: Send + Sync;
type Event: Serialize + DeserializeOwned + Send + Sync;
type Error: Send + Sync;
fn event_store(
&self
) -> &(dyn EventStore<Self::Event, Self::Error> + Send + Sync);
fn apply_event(state: Self::State, event: &Self::Event) -> Self::State;
fn validate_command(
aggregate_state: &AggregateState<Self::State>,
cmd: &Self::Command
) -> Result<(), Self::Error>;
fn handle_command(
&self,
aggregate_state: &AggregateState<Self::State>,
cmd: Self::Command
) -> Vec<Self::Event>ⓘ;
}
Expand description
The Aggregate trait is responsible for validating commands, mapping commands to events, and applying events onto the aggregate 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 purposfully synchronous. If you are implementing this trait, your aggregate
should not have any side effects. If you additional information to handle commands correctly, then
consider either looking up that information and placing it in the command, or if that is not an option
you can implement the more powerful asynchronous AggregateManager
trait - it will then be your responsibility to
uphold the Aggregate invariants.
Associated Types
Required methods
fn event_store(
&self
) -> &(dyn EventStore<Self::Event, Self::Error> + Send + Sync)
fn event_store(
&self
) -> &(dyn EventStore<Self::Event, Self::Error> + Send + Sync)
Event store configured for aggregate - required for the default implementation of AggregateManager
fn apply_event(state: Self::State, event: &Self::Event) -> Self::State
fn apply_event(state: Self::State, event: &Self::Event) -> Self::State
Updates the aggregate state using the new event.
fn validate_command(
aggregate_state: &AggregateState<Self::State>,
cmd: &Self::Command
) -> Result<(), Self::Error>
fn validate_command(
aggregate_state: &AggregateState<Self::State>,
cmd: &Self::Command
) -> Result<(), Self::Error>
Validates a command against the current aggregate state. The aggregate must be able to handle the command if validation succeeds.