Module unit_of_work

Source
Expand description

§UnitOfWork

UnitOfWork is to a unit that manages atomic transaction.

Its executor is supposed to be shared with its sub type Repository.

commit, and rollback, is governed by this implementation.

When events are collected in RepositoryTRepository, you can collect them

automatically thanks to _commit_hook method.

§Usage Pattern
// Intialize Uow, start transaction
let mut uow = UnitOfWork::<Repository<TaskAggregate>, Executor,TaskAggregate>::new(context).await;

// Fetch data
let mut aggregate = uow.repository().get(&cmd.aggregate_id).await?;

// Process business logic
aggregate.process_business_logic(cmd)?;

// Apply changes
uow.repository().update(&mut aggregate).await?;

// Commit transaction
uow.commit::<ServiceOutBox>().await?;

§Handler

Handler is what orchestrates operations from data fetching, business logic operation and store changes back to db. This is where tranasction occurs.

§Example

struct ApplicationHandler;
impl Handler for ApplicationHandler{
    type E = ApplicationExecutor;
    type R = ApplicationRepository<Aggregate>
}

impl ApplicationHandler{
    pub async fn serve_request(
        cmd: Command1,
        context: AtomicContextManager,
) -> Result<(),ServiceError> {
    let mut uow = TaskHandler::uow(context).await;
}

Structs§

UnitOfWork

Traits§

Executor
Executor is abstract implementation of whatever storage layer you use. Among examples are RDBMS, Queue, NoSQLs.
TUnitOfWork