Struct ddd_rs::infrastructure::persistence::DomainRepository
source · pub struct DomainRepository<T: AggregateRoot + Send + 'static, TRepository: Repository<T>, TDomainEventHandler: DomainEventHandler<<T as AggregateRoot>::DomainEvent>> { /* private fields */ }Expand description
A Repository implementation that handles DomainEvents when persisting the AggregateRoot entity.
Examples
use std::sync::{Arc, RwLock};
use ddd_rs::application::{
domain_event_handler::{self, DomainEventHandler},
Repository,
};
use ddd_rs::domain::{AggregateRoot, Entity, UnitDomainEvent};
use ddd_rs::infrastructure::{DomainRepository, InMemoryRepository};
#[derive(ddd_rs::AggregateRoot, ddd_rs::Entity, Clone)]
struct MyEntity {
id: i32,
domain_events: Vec<UnitDomainEvent>,
created_at: chrono::DateTime<chrono::Utc>,
updated_at: chrono::DateTime<chrono::Utc>,
}
impl MyEntity {
pub fn new(id: i32) -> Self {
Self {
id,
domain_events: vec![],
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),
}
}
}
struct MockDomainEventHandler {
calls: Arc<RwLock<i32>>,
}
impl MockDomainEventHandler {
pub fn new(calls: Arc<RwLock<i32>>) -> Self {
Self { calls }
}
}
#[async_trait::async_trait]
impl DomainEventHandler<UnitDomainEvent> for MockDomainEventHandler {
async fn handle(&self, _event: UnitDomainEvent) -> domain_event_handler::Result<()> {
let mut calls = self.calls.write().unwrap();
*calls += 1;
Ok(())
}
}
let calls = Arc::new(RwLock::new(0));
let my_entity_repository = DomainRepository::new(
InMemoryRepository::new(),
MockDomainEventHandler::new(calls.clone()),
);
let mut my_entity = MyEntity::new(1);
my_entity.register_domain_event(UnitDomainEvent);
my_entity.register_domain_event(UnitDomainEvent);
my_entity.register_domain_event(UnitDomainEvent);
my_entity_repository.add(my_entity).await.unwrap();
let calls = *calls.read().unwrap();
assert_eq!(calls, 3);Implementations§
source§impl<T: AggregateRoot, TRepository: Repository<T>, TDomainEventHandler: DomainEventHandler<<T as AggregateRoot>::DomainEvent>> DomainRepository<T, TRepository, TDomainEventHandler>
impl<T: AggregateRoot, TRepository: Repository<T>, TDomainEventHandler: DomainEventHandler<<T as AggregateRoot>::DomainEvent>> DomainRepository<T, TRepository, TDomainEventHandler>
sourcepub fn new(
repository: TRepository,
domain_event_handler: TDomainEventHandler
) -> Self
pub fn new(
repository: TRepository,
domain_event_handler: TDomainEventHandler
) -> Self
Creates a new DomainRepository.
Trait Implementations§
source§impl<T: AggregateRoot, TRepository: Repository<T>, TDomainEventHandler: DomainEventHandler<<T as AggregateRoot>::DomainEvent>> ReadRepository<T> for DomainRepository<T, TRepository, TDomainEventHandler>
impl<T: AggregateRoot, TRepository: Repository<T>, TDomainEventHandler: DomainEventHandler<<T as AggregateRoot>::DomainEvent>> ReadRepository<T> for DomainRepository<T, TRepository, TDomainEventHandler>
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, TRepository: Repository<T>, TDomainEventHandler: DomainEventHandler<<T as AggregateRoot>::DomainEvent>> Repository<T> for DomainRepository<T, TRepository, TDomainEventHandler>
impl<T: AggregateRoot, TRepository: Repository<T>, TDomainEventHandler: DomainEventHandler<<T as AggregateRoot>::DomainEvent>> Repository<T> for DomainRepository<T, TRepository, TDomainEventHandler>
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.