Struct ddd_rs::infrastructure::persistence::InMemoryRepository
source · pub struct InMemoryRepository<T: AggregateRoot> { /* private fields */ }Expand description
An in-memory implementation of Repository, using a HashMap.
Examples
use ddd_rs::application::{ReadRepository, Repository};
use ddd_rs::domain::{AggregateRoot, Entity, UnitDomainEvent};
use ddd_rs::infrastructure::InMemoryRepository;
#[derive(ddd_rs::AggregateRoot, ddd_rs::Entity, Clone)]
struct MyEntity {
id: i32,
my_field: String,
domain_events: Vec<UnitDomainEvent>,
created_at: chrono::DateTime<chrono::Utc>,
updated_at: chrono::DateTime<chrono::Utc>,
}
impl MyEntity {
pub fn new(id: i32, my_field: impl ToString) -> Self {
Self {
id,
my_field: my_field.to_string(),
domain_events: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
}
}
}
let my_entity_repository: InMemoryRepository<MyEntity> = InMemoryRepository::new();
my_entity_repository.add(MyEntity::new(1, "foo")).await.unwrap();
my_entity_repository.add(MyEntity::new(2, "bar")).await.unwrap();
my_entity_repository.add(MyEntity::new(3, "baz")).await.unwrap();
let my_entity_2 = my_entity_repository.get_by_id(2).await.unwrap();
assert!(my_entity_2.is_some());
assert_eq!(my_entity_2.map(|e| e.my_field), Some(String::from("bar")));Implementations§
source§impl<T: AggregateRoot> InMemoryRepository<T>
impl<T: AggregateRoot> InMemoryRepository<T>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new InMemoryRepository.
Trait Implementations§
source§impl<T: AggregateRoot> Default for InMemoryRepository<T>
impl<T: AggregateRoot> Default for InMemoryRepository<T>
source§impl<T: AggregateRoot + Clone> ReadRepository<T> for InMemoryRepository<T>where
<T as Entity>::Id: Hash + Eq,
impl<T: AggregateRoot + Clone> ReadRepository<T> for InMemoryRepository<T>where
<T as Entity>::Id: Hash + Eq,
source§fn get_by_id<'life0, 'async_trait>(
&'life0 self,
id: <T as Entity>::Id
) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_by_id<'life0, 'async_trait>(
&'life0 self,
id: <T as Entity>::Id
) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Gets an entity with the given ID.
source§fn list<'life0, 'async_trait>(
&'life0 self,
skip: usize,
take: usize
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn list<'life0, 'async_trait>(
&'life0 self,
skip: usize,
take: usize
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Lists all entities within a given page.
source§impl<T: AggregateRoot + Clone> Repository<T> for InMemoryRepository<T>where
<T as Entity>::Id: Hash + Eq,
impl<T: AggregateRoot + Clone> Repository<T> for InMemoryRepository<T>where
<T as Entity>::Id: Hash + Eq,
source§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 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.
source§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 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,
Updates an entity on the repository.
source§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,
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,
Deletes the entity from the repository.
source§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 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.