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>

source

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>

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,

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,

Lists all entities within a given page.
source§

fn count<'life0, 'async_trait>(
    &'life0 self
) -> Pin<Box<dyn Future<Output = Result<usize>> + Send + 'async_trait>>where
    Self: 'async_trait,
    'life0: 'async_trait,

Returns the total number of entities in the repository.
source§

fn any<'life0, 'async_trait>(
    &'life0 self
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
    Self: 'async_trait,
    'life0: 'async_trait,

Returns a boolean whether the repository is not empty.
source§

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,

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,

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,

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,

Adds the given entities to the repository.
source§

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,

Updates the given entities on the repository.
source§

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,

Deletes the given entities from the repository.

Auto Trait Implementations§

§

impl<T, TRepository, TDomainEventHandler> RefUnwindSafe for DomainRepository<T, TRepository, TDomainEventHandler>where
    T: RefUnwindSafe,
    TDomainEventHandler: RefUnwindSafe,
    TRepository: RefUnwindSafe,

§

impl<T, TRepository, TDomainEventHandler> Send for DomainRepository<T, TRepository, TDomainEventHandler>

§

impl<T, TRepository, TDomainEventHandler> Sync for DomainRepository<T, TRepository, TDomainEventHandler>

§

impl<T, TRepository, TDomainEventHandler> Unpin for DomainRepository<T, TRepository, TDomainEventHandler>where
    T: Unpin,
    TDomainEventHandler: Unpin,
    TRepository: Unpin,

§

impl<T, TRepository, TDomainEventHandler> UnwindSafe for DomainRepository<T, TRepository, TDomainEventHandler>where
    T: UnwindSafe,
    TDomainEventHandler: UnwindSafe,
    TRepository: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.