[][src]Trait domain_patterns::models::Entity

pub trait Entity: PartialEq {
    fn id(&self) -> String;
fn version(&self) -> u64; fn next_version(&self) -> u64 { ... } }

A trait that defines an Entity, which is any object with a unique and globally persistent identity.

The generic type K should match the same type as the internal globally unique id used for the entity. Be careful when choosing what to return here. The result of id() will be used as the primary key for the entity when communicating with a database via a repository.

Example

use domain_patterns::models::Entity;

struct User {
    id: uuid::Uuid,
    version: u64,
    email: String,
    password: String,
}

impl Entity for User {
    fn id(&self) -> String {
        self.id.to_string()
    }

    fn version(&self) -> u64 {
        self.version
    }
}

impl std::cmp::PartialEq for User {
    fn eq(&self, other: &Self) -> bool {
        &self.id() == &other.id()
    }
}

Required methods

fn id(&self) -> String

id should be the entities globally unique id. It doesn't matter what it is internally as long as that thing can be returned as a string (implements Display from std)

fn version(&self) -> u64

version is a simple integers that is incremented for every mutation. This allows us to have something like an EntityCreated event where we can pass versions in, and re-order the events for playback in the correct order.

Loading content...

Provided methods

fn next_version(&self) -> u64

next_version simply returns the current version incremented by 1. This default implementation should never have to be overriden.

Loading content...

Implementors

Loading content...