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