Skip to main content

ddd_toolkit_core/
aggregate_root.rs

1use crate::{DomainEvent, Entity};
2
3/// **English:**<br>
4/// Represents an [`Entity`] that records domain events produced by state changes.
5///
6/// Implementations own the event queue. [`AggregateRoot::record_event`] adds an event, while
7/// [`AggregateRoot::pull_events`] returns the events selected by the implementation and may clear
8/// them from the queue. The trait does not prescribe ordering, deduplication, or persistence.
9///
10/// **日本語:**<br>
11/// 状態変更によって発生したドメインイベントを記録する [`Entity`] を表します。
12///
13/// 実装型がイベントキューを所有します。[`AggregateRoot::record_event`] はイベントを追加し、
14/// [`AggregateRoot::pull_events`] は実装が選んだイベントを返し、キューから削除する場合があります。
15/// 順序、重複排除、永続化の方法はこのトレイトでは規定しません。
16pub trait AggregateRoot: Entity {
17    /// **English:**<br>
18    /// The domain-event type emitted by this aggregate.
19    ///
20    /// **日本語:**<br>
21    /// この集約が発行するドメインイベントの型です。
22    type Event: DomainEvent;
23
24    /// **English:**<br>
25    /// Records `event` for later retrieval by [`AggregateRoot::pull_events`].
26    ///
27    /// The implementation decides whether recording changes aggregate state, preserves insertion
28    /// order, or rejects a value. The method may panic only if the implementation documents such
29    /// a condition.
30    ///
31    /// **日本語:**<br>
32    /// 後で [`AggregateRoot::pull_events`] から取得できるよう `event` を記録します。
33    ///
34    /// 集約の状態を変更するか、挿入順を保つか、値を拒否するかは実装が決めます。実装が条件を
35    /// 文書化している場合を除き、このメソッドがパニックすることは想定されません。
36    fn record_event(&mut self, event: Self::Event);
37
38    /// **English:**<br>
39    /// Takes the currently recorded events from this aggregate.
40    ///
41    /// Implementations define whether the returned vector is empty when no events are pending and
42    /// whether taking events clears the internal queue. The returned events remain owned by the caller.
43    ///
44    /// **日本語:**<br>
45    /// この集約に現在記録されているイベントを取り出します。
46    ///
47    /// 保留イベントがない場合に空のベクターを返すか、取り出し時に内部キューを空にするかは
48    /// 実装が定義します。返されたイベントの所有権は呼び出し側に移ります。
49    fn pull_events(&mut self) -> Vec<Self::Event>;
50}