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,
impl<T> Root<T>where
T: Aggregate,
sourcepub fn aggregate_id(&self) -> &T::Id
pub fn aggregate_id(&self) -> &T::Id
Returns the unique identifier of the Aggregate.
sourcepub fn to_aggregate_type<K>(&self) -> Kwhere
K: From<T>,
pub fn to_aggregate_type<K>(&self) -> Kwhere
K: From<T>,
sourcepub fn record_new(event: Envelope<T::Event>) -> Result<Self, T::Error>
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.
sourcepub fn record_that(&mut self, event: Envelope<T::Event>) -> Result<(), T::Error>
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.