Skip to main content

AggregateRoot

Trait AggregateRoot 

Source
pub trait AggregateRoot: PersistentEntity {
    type Event: Send + Sync + Clone;

    // Required methods
    fn uncommitted_events(&self) -> &[Self::Event];
    fn clear_events(&mut self);
    fn apply_event(&mut self, event: Self::Event);

    // Provided methods
    fn version(&self) -> Option<u64> { ... }
    fn increment_version(&mut self) { ... }
    fn has_uncommitted_events(&self) -> bool { ... }
    fn uncommitted_event_count(&self) -> usize { ... }
}
Expand description

DDD Aggregate Root trait.

Aggregate Roots are characterized by:

  • Being the only entry point to the aggregate
  • Maintaining consistency boundaries
  • Publishing domain events for state changes
  • Having a unique identity (via PersistentEntity)

Required Associated Types§

Source

type Event: Send + Sync + Clone

The type of domain events this aggregate can produce.

Required Methods§

Source

fn uncommitted_events(&self) -> &[Self::Event]

Get all uncommitted domain events.

These events should be persisted/published after the aggregate is saved to the repository.

Source

fn clear_events(&mut self)

Clear all uncommitted events.

This should be called after events have been persisted/published.

Source

fn apply_event(&mut self, event: Self::Event)

Apply a domain event to update the aggregate state.

This method should:

  1. Update the aggregate’s internal state based on the event
  2. Store the event in the uncommitted events list

Provided Methods§

Source

fn version(&self) -> Option<u64>

Get the aggregate version for optimistic concurrency.

Returns None if versioning is not supported.

Source

fn increment_version(&mut self)

Increment the aggregate version.

Called after successful persistence.

Source

fn has_uncommitted_events(&self) -> bool

Check if there are any uncommitted events.

Source

fn uncommitted_event_count(&self) -> usize

Get the number of uncommitted events.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§