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