Skip to main content

AggregateRoot

Trait AggregateRoot 

Source
pub trait AggregateRoot: Eventsourced {
    type Id: Clone + Eq + Hash + Send + Sync + 'static;

    // Required method
    fn aggregate_id(&self) -> &Self::Id;

    // Provided methods
    fn check_invariants(_state: &Self::State) -> Result<(), Self::Error> { ... }
    fn encode_state(_state: &Self::State) -> Option<Result<Vec<u8>, String>> { ... }
    fn decode_state(_bytes: &[u8]) -> Result<Self::State, String> { ... }
}
Expand description

DDD aggregate root. One per consistency boundary; every command and every event passes through one of these.

Note on bounds. The matching constraints <Self as Eventsourced>::Command: Command<AggregateId = Self::Id> and <Self as Eventsourced>::Event: DomainEvent are not expressed as a supertrait where-clause here, because Rust’s MSRV-stable trait machinery propagates such clauses awkwardly through every usage site. The patterns that actually consume these bounds (e.g. crate::cqrs::CqrsPattern) re-state them at their own builder / impl sites. Do implement crate::Command for your Command type and crate::DomainEvent for your Event type.

Required Associated Types§

Source

type Id: Clone + Eq + Hash + Send + Sync + 'static

The aggregate’s identity type.

Required Methods§

Source

fn aggregate_id(&self) -> &Self::Id

This instance’s id.

Provided Methods§

Source

fn check_invariants(_state: &Self::State) -> Result<(), Self::Error>

Optional invariant check, run after applying a command’s events to the in-memory state. Returning Err causes the framework to surface a crate::PatternError::Invariant after the events were persisted — i.e. it’s a post-condition, not a guard. Use it to detect bugs in command handlers, not to gate writes (gate writes by returning Err from command_to_events instead). Default: always Ok.

Source

fn encode_state(_state: &Self::State) -> Option<Result<Vec<u8>, String>>

Encode State for snapshot persistence. Returning None disables snapshotting for this aggregate even if a snapshot store is configured at the pattern level — recovery falls back to journal replay only. Returning Some(Err(_)) surfaces crate::PatternError::Codec.

Source

fn decode_state(_bytes: &[u8]) -> Result<Self::State, String>

Decode a snapshot payload back into State. Must be the inverse of Self::encode_state. Required iff encode_state is implemented.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§