Skip to content

almin: Transaction of UseCase #219

@azu

Description

@azu

It related with Unit of work #186
Almin's unit of work is a single of UseCase lifecycle.

image

We will introduce the Unit of work as actual class.

illustration code:

context.transaction(unitOfWork => { // begin transaction
    const executor = new UseCaseExecutor(useCase);
    executor.onDispatch(() => unitOfWork.commit()); // store group receive it and try to commit
    executor.onDidExecuted(() => unitOfWork.commit()); // store group receive it and try to commit
    executor.onCompleteExecuted(() => unitOfWork.commit()); // store group receive it and try to commit
    return executor.execute(); // may be returned a promise
}).then(() => {
    // end transation
});

What is make it ?

  • Transaction of UseCase
    • The user can prevent heavy updating of StoreGroup
  • Silent UseCase
    • e.g.) LogUseCase wont to update any Store, but currenly it dispatch didExecuted and completeExected

illustration code

Transaction UseCase that can do manually update.
It help to improve application performance.

  • Context.useCase#exceute try to commit: 2
  • Manually transation try to commit: 1
context.transaction(unitOfWork => { // begin transaction
    return context.useCase(new UseCase).execute().then(() => {
         unitOfWork.commit(); // => try to change StoreGroup
    })
}).then(() => { // automatically release transaction - almin not want to get dead lock.
    // after transaction
});

Example

Difference with Context#useCase:

  • Do not update StoreGroup automatically
  • You should call committer.commit() to update StoreGroup at any time

Example code:

context.transaction("name", transactionContext => {
     return transactionContext.useCase(new ChangeAUseCase()).execute() // no update store
         .then(() => {
             return transactionContext.useCase(new ChangeBUseCase()).execute(); // no update store
         }).then(() => {
             return transactionContext.useCase(new ChangeCUseCase()).execute(); // no update store
         }).then(() => {
             transactionContext.commit(); // update store
             // replay: ChangeAUseCase -> ChangeBUseCase -> ChangeCUseCase
         });
 })

FAQ

  • Q. Why automatically release the transation
  • A. Almin do not dead lock
  • Q. Why the transaction prevent to update each Stores.
  • A. Almin apply CQRS.
    • CQRS apply Eventual consistency
    • The view of application only know updating of StoreGroup
    • In other word, the view don't know updating of each Stores
    • The transaction aim to stop updating of StoreGroup
    • In other word, the transation aim to improve the view performance
  • Q. Can Child UseCase context use transaction?
  • A. No. The transaction handle a root UseCase. The root UseCase includes children UseCase.
    • Child UseCase context is under the root transaction

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions