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