Skip to main content

InMemoryStore

Struct InMemoryStore 

Source
pub struct InMemoryStore<A>
where A: AggregateRoot,
{ /* private fields */ }
Expand description

A Load/Save/Delete implementation backed by an in-memory HashMap, with last-write-wins semantics (see Save’s docs for what that means for PortErrorKind::Conflict).

§Examples

See Save for a worked example of saving and loading through an InMemoryStore. The example below covers the store’s own inspection methods, useful for asserting on what a use case persisted.

use ddd_toolkit_core::domain::{AggregateRoot, DomainEvent, Entity, EntityId, ValueObject};
use ddd_toolkit_core::mock::repository::InMemoryStore;
use ddd_toolkit_core::port::repository::Save;
use std::fmt::Display;

let store = InMemoryStore::new();
assert!(store.is_empty());

let mut order = Order { id: OrderId(1), events: vec![OrderPlaced] };
block_on(store.save(&mut order))?;

assert_eq!(store.len(), 1);

// events recorded across every save() call so far, for asserting a use
// case published what it was supposed to
assert_eq!(store.take_recorded_events(), vec![OrderPlaced]);
assert_eq!(store.take_recorded_events(), vec![]); // drained

Implementations§

Source§

impl<A> InMemoryStore<A>
where A: AggregateRoot,

Source

pub fn new() -> InMemoryStore<A>

Creates a new, empty store.

Source

pub fn take_recorded_events(&self) -> Vec<<A as AggregateRoot>::Event>

Returns and clears every event recorded by a save call so far.

Source

pub fn len(&self) -> usize

The number of aggregates currently stored.

Source

pub fn is_empty(&self) -> bool

Whether the store currently holds no aggregates.

Trait Implementations§

Source§

impl<A> Debug for InMemoryStore<A>
where A: Debug + AggregateRoot,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<A> Default for InMemoryStore<A>
where A: AggregateRoot,

Source§

fn default() -> InMemoryStore<A>

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

impl<A> Delete<A> for InMemoryStore<A>
where A: AggregateRoot + Clone,

Source§

async fn delete(&self, id: &<A as Entity>::Id) -> Result<(), PortError>

Deletes the aggregate with the given id. See the trait-level docs for why deleting a missing id is not an error.
Source§

impl<A> Load<A> for InMemoryStore<A>
where A: AggregateRoot + Clone,

Source§

async fn load(&self, id: &<A as Entity>::Id) -> Result<Option<A>, PortError>

Returns the aggregate with the given id, or None if it doesn’t exist.
Source§

impl<A> Save<A> for InMemoryStore<A>
where A: AggregateRoot + Clone,

Source§

async fn save(&self, aggregate: &mut A) -> Result<(), PortError>

Persists the aggregate, draining and recording any events it has accumulated via AggregateRoot::take_events. See the trait-level docs for the Conflict contract.

Auto Trait Implementations§

§

impl<A> !Freeze for InMemoryStore<A>

§

impl<A> RefUnwindSafe for InMemoryStore<A>
where Mutex<StoreState<A>>: RefUnwindSafe,

§

impl<A> Send for InMemoryStore<A>
where Mutex<StoreState<A>>: Send,

§

impl<A> Sync for InMemoryStore<A>
where Mutex<StoreState<A>>: Sync,

§

impl<A> Unpin for InMemoryStore<A>
where Mutex<StoreState<A>>: Unpin,

§

impl<A> UnsafeUnpin for InMemoryStore<A>
where Mutex<StoreState<A>>: UnsafeUnpin,

§

impl<A> UnwindSafe for InMemoryStore<A>
where Mutex<StoreState<A>>: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.