pub trait Repository<T: AggregateRoot>: ReadRepository<T> {
// Required methods
fn add<'life0, 'async_trait>(
&'life0 self,
entity: T,
) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn update<'life0, 'async_trait>(
&'life0 self,
entity: T,
) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn delete<'life0, 'async_trait>(
&'life0 self,
entity: T,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn add_range<'life0, 'async_trait>(
&'life0 self,
entities: Vec<T>,
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait { ... }
fn update_range<'life0, 'async_trait>(
&'life0 self,
entities: Vec<T>,
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait { ... }
fn delete_range<'life0, 'async_trait>(
&'life0 self,
entities: Vec<T>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: Sync + 'async_trait,
'life0: 'async_trait { ... }
}
Expand description
Trait for representing a Repository.
Therefore, use a Repository, the purpose of which is to encapsulate all the logic needed to obtain object references. The domain objects won’t have to deal with the infrastructure to get the needed references to other objects of the domain. They will just get them from the Repository and the model is regaining its clarity and focus.
§Examples
This example uses the InMemoryRepository which is a sample implementation of this trait.
use ddd_rs::{
application::{ReadRepository, Repository},
infrastructure::InMemoryRepository
};
// By definition, only `AggregateRoot`s have repositories.
//
// Common entities must be retrieved and persisted through their associated aggregate roots.
#[derive(ddd_rs::AggregateRoot, ddd_rs::Entity, Clone)]
struct MyEntity {
#[entity(id)]
id: u32,
my_field: String,
}
impl MyEntity {
pub fn new(id: u32, my_field: impl ToString) -> Self {
Self {
id,
my_field: my_field.to_string(),
}
}
}
let repository: InMemoryRepository<MyEntity> = InMemoryRepository::new();
// Add some entities to the repository.
repository.add(MyEntity::new(1, "foo")).await.unwrap();
repository.add(MyEntity::new(2, "bar")).await.unwrap();
repository.add(MyEntity::new(3, "baz")).await.unwrap();
// Attempt to retrieve an entity by its ID.
let my_entity_2 = repository.get_by_id(2).await.unwrap();
assert!(my_entity_2.is_some());
assert_eq!(my_entity_2.as_ref().map(|e| e.my_field.as_str()), Some("bar"));
let mut my_entity_2 = my_entity_2.unwrap();
// Update the entity, then persist its changes.
my_entity_2.my_field = "qux".to_string();
let my_entity_2 = repository.update(my_entity_2).await.unwrap();
assert_eq!(my_entity_2.my_field.as_str(), "qux");
// Delete the entity permanently.
repository.delete(my_entity_2).await.unwrap();
// Assert it no longer exists.
assert!(!repository.exists(2).await.unwrap());
Required Methods§
Sourcefn add<'life0, 'async_trait>(
&'life0 self,
entity: T,
) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn add<'life0, 'async_trait>(
&'life0 self,
entity: T,
) -> Pin<Box<dyn Future<Output = Result<T>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Adds an entity to the repository.
Provided Methods§
Sourcefn add_range<'life0, 'async_trait>(
&'life0 self,
entities: Vec<T>,
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
fn add_range<'life0, 'async_trait>(
&'life0 self,
entities: Vec<T>,
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
Adds the given entities to the repository.