Module ruva_core::unit_of_work
source · Expand description
§TUnitOfWork
TUnitOfWork is a trait to manage atomic transaction.
commit
, and rollback
, is governed by this implementation.
Concrete implementation that implements TRepository may also implement TUnitOfWork
To make sure that events raised in Aggregate
object is properly collected, you want to implement
TCommitHook as well.
§Usage Pattern
// Service Handler
pub struct CustomHandler<R> {
_r: PhantomData<R>,
}
impl<R> CustomHandler<R>
where
R: TCustomRepository + TUnitOfWork,
{
pub async fn create_aggregate(
cmd: CreateCommand,
mut uow: R,
) -> Result<CustomResponse, CustomError> {
// Transation begin
uow.begin().await?;
let mut aggregate: CustomAggregate = CustomAggregate::new(cmd);
uow.add(&mut aggregate).await?;
// Transation commit
uow.commit().await?;
Ok(aggregate.id.into())
}
}
Note that if you don’t “attatch” TUnitOfWork, the uow
above would only have an access to TRepository but not transation-related methods.
Traits§
- Template for Unit of Work Concrete implementation must implement
_commit
method If you want to add hooks on events, you can implementprocess_internal_events
andprocess_external_events