Shop: Compose Services in One Call
Chapters
- Who owns what
- A service made of other services
- Follow the sale
- A child's lifetime belongs to its parent
Transcript
Who owns what
Branches keeps no fields of its own. It keeps a map of child modules, one inventory per branch number. Opening branch nine checks that the branch is new and that the Shop has fewer than thirty-two. Then it creates an inventory and attaches it under key nine. To stock an item, Branches finds the inventory for that branch and calls its put method. Branches cannot see inside the inventory's data. Inventory checks its own limit and saves item forty-four at price one hundred with eight in stock.
A service made of other services
Checkout saves nothing, so it has no memory folder. Its service lists the two services it depends on. The root builds Branches and Settings once and hands the same two to Checkout.
Follow the sale
A customer checks out three units of item forty-four from branch nine. The endpoint opens one write scope and calls buy. Everything that follows happens inside that one scope. Buy first asks Branches for a quote. Branches passes the question to the inventory for branch nine. Quote only reads. Eight are in stock, so three at one hundred gives three hundred. Next it reads the tax from Settings, which is five hundred basis points. The arithmetic is checked. A total that does not fit returns None. Here the total is three hundred and fifteen. Only now does checkout ask for the sale. Inventory checks the quantity once more and takes three from stock. Five remain. Checkout returns three hundred and fifteen and the scope closes. The test then asks for six. Five are left, so the quote comes back empty and buy returns None before it reads tax or sells. This order matters. A returned refusal does not undo a write that already happened, so the checks come first.
A child's lifetime belongs to its parent
Closing a branch takes its inventory out of the map. Nothing attaches it anywhere else, so it is gone when the scope ends. Opening branch nine again creates a new, empty inventory. Item forty-four is no longer there.