Trait cqrs_es2::ICommandHandler [−][src]
pub trait ICommandHandler<C: ICommand, E: IEvent> { fn handle(&self, command: C) -> Result<Vec<E>, AggregateError>; }
Expand description
Command handlers are usually the aggregates. It consumes the command and emit all events resulting from this command.
Example
For illustration only:
use cqrs_es2::{ example_impl::{ AddressUpdated, CustomerCommand, CustomerEvent, EmailUpdated, NameAdded, }, AggregateError, ICommandHandler, }; pub struct CustomerCommandHandler {}; impl ICommandHandler<CustomerCommand, CustomerEvent> for CustomerCommandHandler { fn handle( &self, command: CustomerCommand, ) -> Result<Vec<CustomerEvent>, AggregateError> { match command { CustomerCommand::AddCustomerName(payload) => { let payload = NameAdded { changed_name: payload.changed_name, }; Ok(vec![CustomerEvent::NameAdded(payload)]) }, CustomerCommand::UpdateEmail(payload) => { let payload = EmailUpdated { new_email: payload.new_email, }; Ok(vec![CustomerEvent::EmailUpdated( payload, )]) }, CustomerCommand::AddAddress(payload) => { let payload = AddressUpdated { new_address: payload.new_address, }; Ok(vec![CustomerEvent::AddressUpdated( payload, )]) }, } } }
Required methods
fn handle(&self, command: C) -> Result<Vec<E>, AggregateError>
fn handle(&self, command: C) -> Result<Vec<E>, AggregateError>
handle inbound command and return a vector of events or an error