Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean Architecture - Robert Martin - Use Case Granularity

I am considering implementing Robert Martin's Clean Architecture in a project and I am trying to find out how to handle non-trivial use cases.

I am finding it difficult to scale the architecture to complex/composed use cases, especially use cases where the actor is the system as opposed to a user, as in system performing some sort of batch processing.

For illustration purposes, let's assume a use case like "System updates all account balances" implemented in pseudocode like

class UpdateAllAccountBalancesInteraction {
    function Execute() {
        Get a list of all accounts
        For each account
            Get a list of all new transactions for account
            For each transaction
                Perform some specific calculation on the transaction
            Update account balance
    }
}

In addition, "Get a list of all accounts", "Get a list of all new transactions for account", "Perform some specific calculation on the transaction", "Update account balance" are all valid use cases of their own and each of them is already implemented in its own interaction class.

A few questions arise:

  • Is the use case "System updates all account balances" even a valid use case or should it be broken down into smaller use cases (although from a business prospective it seems to make sense, it is a legitimate business scenario)?
  • Is UpdateAllAccountBalancesInteraction a legitimate interaction?
  • Is an interaction allowed to/supposed to orchestrate other interactions?
  • Is code that orchestrates other interactions really belonging somewhere else?
  • Is it just OK to have UpdateAllAccountBalancesInteraction as an interaction, but have it call functions shared by the other interactors rather than act as an orchestrator of other interactors?
like image 501
PlusInfinite Avatar asked Jan 29 '16 15:01

PlusInfinite


1 Answers

Clearly, you have a new for high level interactions that share some (or a lot of) common functionality with lower level interactions. This is ok.

If the business requires a use case called UpdateAllAccountBalances, then it is a valid use case, and it's good that you're naming it in a way that reflects the business logic.

It's o.k. for one interaction to call other interactions, if this reflects your business logic accurately. Ask yourself the following question: If the requirements for UpdateAccountBalance change, should this also affect UpdateAllAccountBalances in exactly the same way? If the answer is yes, then the best way to achieve this is to have UpdateAllAccountBalances call UpdateAccountBalance, because otherwise, you'll need to make a change in two places in order to keep them consistent. If the answer is no, then you want to decouple the two interactions, and this can be done by having them call shared functions.

like image 100
Adi Levin Avatar answered Nov 13 '22 06:11

Adi Levin