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