ddd_toolkit_core/repository.rs
1use crate::{AggregateRoot, Entity};
2
3/// **English:** Defines persistence operations for one [`AggregateRoot`] type.
4///
5/// Implementations choose the storage backend and error type. The trait does not prescribe
6/// transaction boundaries, consistency guarantees, identifier ownership, or whether operations
7/// are synchronous. A repository may return [`None`] from [`Repository::find_by_id`] when no
8/// aggregate matches an identifier; storage failures are reported through [`Repository::Error`].
9///
10/// **日本語:** 1 つの [`AggregateRoot`] 型に対する永続化操作を定義します。
11///
12/// ストレージバックエンドとエラー型は実装が選択します。トランザクション境界、整合性保証、
13/// 識別子の所有権、操作が同期的かどうかはこのトレイトでは規定しません。識別子に一致する
14/// 集約がない場合、リポジトリは [`Repository::find_by_id`] から [`None`] を返せます。ストレージ
15/// の失敗は [`Repository::Error`] を通じて報告します。
16pub trait Repository {
17 /// **English:** The aggregate type managed by this repository.
18 ///
19 /// **日本語:** このリポジトリが管理する集約の型です。
20 type Aggregate: AggregateRoot;
21
22 /// **English:** The error type returned when a repository operation fails.
23 ///
24 /// **日本語:** リポジトリ操作が失敗したときに返すエラー型です。
25 type Error;
26
27 /// **English:** Loads the aggregate identified by `id`.
28 ///
29 /// Returns `Ok(None)` when no aggregate matches `id`, or `Err` when the implementation cannot
30 /// complete the lookup. The returned aggregate is owned by the caller.
31 ///
32 /// **日本語:** `id` で識別される集約を読み込みます。
33 ///
34 /// `id` に一致する集約がない場合は `Ok(None)`、実装が検索を完了できない場合は `Err` を
35 /// 返します。返された集約の所有権は呼び出し側に移ります。
36 fn find_by_id(
37 &self,
38 id: &<Self::Aggregate as Entity>::Id,
39 ) -> Result<Option<Self::Aggregate>, Self::Error>;
40
41 /// **English:** Persists the current state of `aggregate`.
42 ///
43 /// The implementation defines whether this inserts, updates, or otherwise stores the aggregate.
44 /// It returns `Err` if persistence fails.
45 ///
46 /// **日本語:** `aggregate` の現在の状態を永続化します。
47 ///
48 /// 挿入、更新、その他の保存のいずれになるかは実装が定義します。永続化に失敗した場合は
49 /// `Err` を返します。
50 fn save(&mut self, aggregate: &Self::Aggregate) -> Result<(), Self::Error>;
51
52 /// **English:** Removes the aggregate identified by `id`.
53 ///
54 /// The implementation defines the behavior when no matching aggregate exists. It returns `Err`
55 /// if deletion cannot be completed.
56 ///
57 /// **日本語:** `id` で識別される集約を削除します。
58 ///
59 /// 一致する集約が存在しない場合の動作は実装が定義します。削除を完了できない場合は `Err`
60 /// を返します。
61 fn delete(&mut self, id: &<Self::Aggregate as Entity>::Id) -> Result<(), Self::Error>;
62
63 /// **English:** Checks whether an aggregate identified by `id` exists.
64 ///
65 /// Returns `Ok(true)` when the aggregate exists and `Ok(false)` otherwise. It returns `Err` if
66 /// the implementation cannot determine the result.
67 ///
68 /// **日本語:** `id` で識別される集約が存在するか確認します。
69 ///
70 /// 集約が存在する場合は `Ok(true)`、それ以外は `Ok(false)` を返します。結果を判定できない
71 /// 場合は `Err` を返します。
72 fn exists(&self, id: &<Self::Aggregate as Entity>::Id) -> Result<bool, Self::Error>;
73}