Skip to main content

ddd_toolkit_core/
async_repository.rs

1#![allow(clippy::type_complexity)]
2
3use crate::{AggregateRoot, Entity};
4use std::future::Future;
5use std::pin::Pin;
6
7/// **English:**<br>
8/// Provides asynchronous persistence operations for one aggregate type. Implementors
9/// supply storage behavior and its error type; returned futures borrow the repository and are
10/// `Send`.
11///
12/// **日本語:**<br>
13/// ひとつの集約型に対する非同期の永続化操作を提供します。実装側が保存処理とエラー型を
14/// 定義します。返される future はリポジトリを借用し、`Send` です。
15pub trait AsyncRepository {
16    /// **English:**<br>
17    /// Aggregate type managed by this repository.
18    ///
19    /// **日本語:**<br>
20    /// このリポジトリが管理する集約型です。
21    type Aggregate: AggregateRoot;
22    /// **English:**<br>
23    /// Error returned by persistence operations.
24    ///
25    /// **日本語:**<br>
26    /// 永続化操作が返すエラー型です。
27    type Error;
28
29    /// **English:**<br>
30    /// Loads an aggregate by identifier, returning `Ok(None)` when no matching
31    /// aggregate exists and an error when the lookup fails.
32    ///
33    /// **日本語:**<br>
34    /// 識別子で集約を読み込みます。一致する集約がなければ `Ok(None)`、検索に失敗すれば
35    /// エラーを返します。
36    fn find_by_id<'a>(
37        &'a self,
38        id: &'a <Self::Aggregate as Entity>::Id,
39    ) -> Pin<Box<dyn Future<Output = Result<Option<Self::Aggregate>, Self::Error>> + Send + 'a>>;
40
41    /// **English:**<br>
42    /// Persists `aggregate`, returning an error if storage fails. Whether this inserts
43    /// or updates is determined by the implementation.
44    ///
45    /// **日本語:**<br>
46    /// `aggregate` を永続化し、保存に失敗した場合はエラーを返します。新規登録か更新かは
47    /// 実装によって決まります。
48    fn save<'a>(
49        &'a mut self,
50        aggregate: &'a Self::Aggregate,
51    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'a>>;
52
53    /// **English:**<br>
54    /// Removes the aggregate identified by `id`; returns an error if the operation
55    /// fails. Behavior when the identifier is absent is implementation-defined.
56    ///
57    /// **日本語:**<br>
58    /// `id` で識別される集約を削除します。操作に失敗すればエラーを返し、識別子が存在しない
59    /// 場合の動作は実装によって異なります。
60    fn delete<'a>(
61        &'a mut self,
62        id: &'a <Self::Aggregate as Entity>::Id,
63    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'a>>;
64
65    /// **English:**<br>
66    /// Checks whether an aggregate with `id` exists, returning its presence or an
67    /// error if the check fails.
68    ///
69    /// **日本語:**<br>
70    /// `id` の集約が存在するかを調べ、その有無または確認に失敗した場合のエラーを返します。
71    fn exists<'a>(
72        &'a self,
73        id: &'a <Self::Aggregate as Entity>::Id,
74    ) -> Pin<Box<dyn Future<Output = Result<bool, Self::Error>> + Send + 'a>>;
75}