Skip to main content

Aggregate

Derive Macro Aggregate 

Source
#[derive(Aggregate)]
{
    // Attributes available to this derive:
    #[aggregate]
}
Expand description

Derives the Aggregate trait for a struct.

This macro generates:

  • An event enum containing all aggregate event types
  • EventKind trait implementation for runtime kind dispatch
  • ProjectionEvent trait implementation for event deserialisation
  • From<E> implementations for each event type
  • Aggregate trait implementation that dispatches to Apply<E> for events

Note: Commands are handled via individual Handle<C> trait implementations. No command enum is generated - use execute_command::<Aggregate, Command>() directly.

§Attributes

§Required

  • id = Type - Aggregate ID type
  • error = Type - Error type for command handling
  • events(Type1, Type2, ...) - Event types

§Optional

  • kind = "name" - Aggregate type identifier (default: lowercase struct name)
  • event_enum = "Name" - Override generated event enum name (default: {Struct}Event)
  • derives(Trait1, Trait2, ...) - Additional derives for the generated event enum. Always includes Clone and serde::Serialize. Common additions: Debug, PartialEq, Eq

§Example

#[derive(Aggregate)]
#[aggregate(
    id = String,
    error = String,
    events(FundsDeposited, FundsWithdrawn),
    derives(Debug, PartialEq, Eq)
)]
pub struct Account {
    balance: i64,
}