Struct eventually::aggregate::Root

source ·
pub struct Root<T>
where T: Aggregate,
{ /* private fields */ }
Expand description

An Aggregate Root represents the Domain Entity object used to load and save an Aggregate from and to a Repository, and to perform actions that may result in new Domain Events to change the state of the Aggregate.

The Aggregate state and list of Domain Events recorded are handled by the Root object itself.

#[derive(Debug, Clone)]
struct MyAggregate {
    // Here goes the state of the Aggregate.
};

#[derive(Debug, Clone, PartialEq, Eq)]
enum MyAggregateEvent {
    // Here we list the Domain Events for the Aggregate.
    EventHasHappened,
}

impl Aggregate for MyAggregate {
    type Id = i64; // Just for the sake of the example.
    type Event = MyAggregateEvent;
    type Error = (); // Just for the sake of the example. Use a proper error here.

    fn aggregate_id(&self) -> &Self::Id {
        todo!()
    }

    fn apply(this: Option<Self>, event: Self::Event) -> Result<Self, Self::Error> {
        todo!()
    }
}

// This type is necessary in order to create a new vtable
// for the method implementations in the block below.
#[derive(Debug, Clone)]
struct MyAggregateRoot(Root<MyAggregate>)

impl MyAggregateRoot {
    pub fn do_something() -> Result<MyAggregate, ()> {
        // Here, we record a new Domain Event through the Root<MyAggregate> object.
        //
        // This will record the new Domain Event in a list of events to commit,
        // and call the `MyAggregate::apply` method to create the Aggregate state.
        Root::<MyAggregate>::record_new(MyAggregateEvent::EventHasHappened)
            .map(MyAggregateRoot)
    }
}

Implementations§

source§

impl<T> Root<T>
where T: Aggregate,

source

pub fn version(&self) -> Version

Returns the current version for the Aggregate.

source

pub fn aggregate_id(&self) -> &T::Id

Returns the unique identifier of the Aggregate.

source

pub fn to_aggregate_type<K>(&self) -> K
where K: From<T>,

Maps the Aggregate value contained within Root to a different type, that can be converted through From trait.

Useful to convert an Aggregate type to a data transfer object to use for database storage.

source

pub fn record_new(event: Envelope<T::Event>) -> Result<Self, T::Error>

Creates a new Aggregate Root instance by applying the specified Domain Event.

Example of usage:

use eventually::{
    event,
    aggregate::Root,
    aggregate,
};

let my_aggregate_root = MyAggregateRoot::record_new(
    event::Envelope::from(MyDomainEvent { /* something */ })
 )?;
§Errors

The method can return an error if the event to apply is unexpected given the current state of the Aggregate.

source

pub fn record_that(&mut self, event: Envelope<T::Event>) -> Result<(), T::Error>

Records a change to the Aggregate Root, expressed by the specified Domain Event.

Example of usage:

use eventually::{
    event,
    aggregate::Root,
};

impl MyAggregateRoot {
    pub fn update_name(&mut self, name: String) -> Result<(), MyAggregateError> {
        if name.is_empty() {
            return Err(MyAggregateError::NameIsEmpty);
        }

        self.record_that(
            event::Envelope::from(MyAggergateEvent::NameWasChanged { name })
        )
    }
}
§Errors

The method can return an error if the event to apply is unexpected given the current state of the Aggregate.

Trait Implementations§

source§

impl<T> Clone for Root<T>
where T: Aggregate + Clone, T::Event: Clone,

source§

fn clone(&self) -> Root<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Root<T>
where T: Aggregate + Debug, T::Event: Debug,

source§

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

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

impl<T> Deref for Root<T>
where T: Aggregate,

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> PartialEq for Root<T>

source§

fn eq(&self, other: &Root<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> StructuralPartialEq for Root<T>
where T: Aggregate,

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Root<T>

§

impl<T> Send for Root<T>

§

impl<T> Sync for Root<T>

§

impl<T> Unpin for Root<T>
where T: Unpin, <T as Aggregate>::Event: Unpin,

§

impl<T> UnwindSafe for Root<T>
where T: UnwindSafe, <T as Aggregate>::Event: 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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more