[][src]Trait domain_patterns::collections::Repository

pub trait Repository<T: AggregateRoot> {
    type Error: Error + Display + 'static;
    fn insert(&mut self, entity: &T) -> Result<Option<String>, Self::Error>;
fn get(&mut self, key: &String) -> Result<Option<T>, Self::Error>;
fn get_paged(
        &mut self,
        page_num: usize,
        page_size: usize
    ) -> Result<Option<Vec<T>>, Self::Error>;
fn update(&mut self, entity: &T) -> Result<Option<String>, Self::Error>;
fn remove(&mut self, key: &String) -> Result<Option<String>, Self::Error>; fn contains_key(&mut self, key: &String) -> Result<bool, Self::Error> { ... } }

A trait that provides a collection like abstraction over database access.

Generic T is some struct that implements Entity<K> where K is used as the key in the repository methods. In other words it's expected that an entities id is used as the key for insert and retrieval.

Associated Types

type Error: Error + Display + 'static

An error that communicates that something went wrong at the database level.

Loading content...

Required methods

fn insert(&mut self, entity: &T) -> Result<Option<String>, Self::Error>

Inserts an entity into the underlying persistent storage (MySQL, Postgres, Mongo etc.).

Entity should be inserted at it's globally unique id. It implements the Entity interface, so it's globally unique id can be accessed by calling id().

If the underlying storage did not have this key present, then insert is successful and the primary key is returned. This allows for auto-generated ids to be returned after insert.

If the underlying storage does have the key present, then None is returned.

Failure case

If we fail to communicate with the underlying storage, then an error is returned.

fn get(&mut self, key: &String) -> Result<Option<T>, Self::Error>

Returns the entity corresponding to the supplied key as an owned type.

Failure case

If we fail to communicate with the underlying storage, then an error is returned.

fn get_paged(
    &mut self,
    page_num: usize,
    page_size: usize
) -> Result<Option<Vec<T>>, Self::Error>

Returns a Vec<T> of entities, based on the supplied page_num and page_size. The page_num should start at 1, but is up to the implementer to design as they see fit.

Failure case

If we fail to communicate with the underlying storage, then an error is returned.

fn update(&mut self, entity: &T) -> Result<Option<String>, Self::Error>

Updates the entity in the underlying storage mechanism and if successful returns the primary key to the caller, which because it implements Entity can be had for free by calling .id() on the entity. If the entity does not exist in the database (it's unique id is not in use), then we return None.

Failure case

If we fail to communicate with the underlying storage, then an error is returned.

fn remove(&mut self, key: &String) -> Result<Option<String>, Self::Error>

Removes an entity from the underlying storage at the given key, returning the entity key if it was in the database and deleted, and otherwise returning None if the entity was not found (no rows effected by the operation).

Failure case

If we fail to communicate with the underlying storage, then an error is returned.

Loading content...

Provided methods

fn contains_key(&mut self, key: &String) -> Result<bool, Self::Error>

Returns true if the underlying storage contains an entity at the specified key, and otherwise returns false.

Failure case

If we fail to communicate with the underlying storage, then an error is returned.

Loading content...

Implementors

Loading content...