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::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());

Implementations§

source§

impl<T: AggregateRoot> InMemoryRepository<T>

source

pub fn new() -> Self

Creates a new InMemoryRepository.

Trait Implementations§

source§

impl<T: AggregateRoot> Default for InMemoryRepository<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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,

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 exists<'life0, 'async_trait>( &'life0 self, id: <T as Entity>::Id ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait,

Checks whether an entity with the given ID exists in the repository.
source§

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

Checks if the repository is empty.
source§

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,

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> RefUnwindSafe for InMemoryRepository<T>

§

impl<T> Send for InMemoryRepository<T>

§

impl<T> Sync for InMemoryRepository<T>

§

impl<T> Unpin for InMemoryRepository<T>where T: Unpin, <T as Entity>::Id: Unpin,

§

impl<T> UnwindSafe for InMemoryRepository<T>

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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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.
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.
source§

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

Performs the conversion.