atomr_patterns/ddd/repository.rs
1//! [`Repository`] — public dispatch surface for commands.
2//!
3//! The `Repository` is what application code holds. It hides the
4//! routing (which actor gets the command, whether it's local or
5//! sharded) and surfaces a clean async fn the user invokes per command.
6
7use async_trait::async_trait;
8
9use atomr_persistence::Eventsourced;
10
11use crate::{AggregateRoot, PatternError};
12
13/// Send commands to an aggregate; receive the events that resulted (or
14/// the typed domain error if the command was rejected).
15///
16/// Implementations are produced by `CqrsPattern::builder().build().materialize()`
17/// — users don't normally implement this trait by hand.
18#[async_trait]
19pub trait Repository: Send + Sync {
20 /// The aggregate this repository routes commands to.
21 type Aggregate: AggregateRoot;
22
23 /// Dispatch a command. Returns the events produced (and persisted)
24 /// on success, or a [`PatternError`] wrapping the domain error / a
25 /// transport / journal failure.
26 async fn send(
27 &self,
28 cmd: <Self::Aggregate as Eventsourced>::Command,
29 ) -> Result<
30 Vec<<Self::Aggregate as Eventsourced>::Event>,
31 PatternError<<Self::Aggregate as Eventsourced>::Error>,
32 >;
33}