Aggregate

Let's start with an aggregate where we can store the state information needed make decisions based on our business rules. For our bank account example we will call our aggregate BankAccount. And for our simple set of business rules, we will only need to give the aggregate a single field, balance.

#[derive(Serialize, Deserialize)]
struct BankAccount {
    balance: f64,
}

In order to operate within the cqrs-es framework, we will need to derive the serde traits, Serialize and Deserialize, and to implement two traits, Default and cqrs_es::Aggregate.

impl Aggregate for BankAccount {
    fn aggregate_type() -> &'static str {
        "account"
    }
}

impl Default for BankAccount {
    fn default() -> Self {
        BankAccount {
            balance: 0_f64
        }
    }
}

The cqrs_es::Aggregate trait provides a name for the aggregate type to distinguish it from any other aggregate.