Deciding where a decision belongs
What you want to do
The shop will keep gaining rules, and for each new one you want to know where it belongs: in a step, in a rescue step, in the workflow or in a policy.
The code
Every decision the shop has made so far has its place. The workflow class, as chapter 37's program has it, names the steps, their policies, the rescue step and the retry budget in its annotations. Its methods are the role implementations of chapter 19 and chapter 23, the ID generator of chapter 11, the input adapter of chapter 21, and the providers of chapter 12, chapter 13, chapter 22 and chapter 35:
/**
* The shop's workflow: it looks the order up, and finishes an order already completed. Otherwise it
* works out what the order costs, then charges it, repairing a charge that fails and auditing one
* that succeeds, then ships, and shipping may be tried once more. Once a journey has ended, the
* customer is told, through the channel the shop configures.
*/
@Workflow(
name = "orders",
reporters = {Log.class, Desk.class},
policies = {Notify.class})
@WithStep(
step = Lookup.class,
name = "lookup",
policies = {AlreadyCompleted.class})
@WithStep(step = Price.class, name = "price")
@WithStep(
step = Charge.class,
name = "charge",
rescue = RenewCard.class,
policies = {Audit.class, Outage.class})
@WithStep(
step = Ship.class,
name = "ship",
retryBudget = 1,
policies = {FailedShipping.class})
final class Orders implements ItineraWorkflow, Messages, Refunds {
private final String channel;
Orders(String channel) {
this.channel = channel;
}
@Override
public void tell(String customer, String message) {
System.out.println(channel + " to " + customer + ": " + message);
}
@Override
public void refund(String receipt, long amount) {
System.out.println("the bank gives " + amount + " back on receipt " + receipt);
}
/** The bank, as the payment client of the journey. */
@Provider(key = "payments")
public Provided<Payments> bank() {
return provide(new Bank(2026));
}
/** The bank's card registry in Europe, for the charge's rescue step only. */
@Provider(steps = {"charge_rescue"})
public Provided<Cards> cards() {
return provide(BankLibrary.cardRegistry("eu"), "eu");
}
/** A carrier for every attempt of a step: the main carrier first, the backup carrier after. */
@Provider(perAttempt = true, key = "carrier")
public Provided<Carrier> carriers(Attempt attempt) {
return attempt.isFirst() ? provide(new MainCarrier()) : provide(new BackupCarrier());
}
/**
* Gives the shipping the weight of the order's parcel, as the warehouse describes it. Every other
* input of the shipping is read from the data bag.
*/
@InputAdapter(steps = {"ship"})
public Supplied parcels(@Key String key, @FromWorkflow Parcel parcel) {
return key.equals("weight") ? supply(parcel.weight()) : fromDataBag();
}
/** Names each journey after its order. */
@IdGenerator
public String orderNumber(DataBag data) {
String order = data.read("order", TypeToken.of(String.class)).orNull();
if (order == null) throw new IllegalStateException("an order without a number");
return order;
}
/** The order book, which has the orders completed before today. */
@Provider(key = "orderBook")
public Provided<OrderBook> ledger() {
return provide(new Ledger(List.of("A-13")));
}
}
What it names is as earlier chapters show it: the lookup, AlreadyCompleted and OrderBook in chapter 22, the pricing in chapter 20, the charge in chapter 33, RenewCard, Payments, Cards and cardRegistry in chapter 35, Audit and Outage in chapter 34, the shipping in chapter 17, Carrier in chapter 12, with the carriers in chapter 13, Notify and Messages in chapter 19, FailedShipping and Refunds in chapter 23, Parcel in chapter 21, the log in chapter 33, chapter 35 and chapter 36, and the desk in chapter 11. Nothing runs in this chapter, so nothing is printed.
| Decision | Where it lives | Why there |
|---|---|---|
| Whether the order was already completed | the lookup step |
reading the order book is the business's work |
| Ending the journey of an order already completed | the alreadyCompleted policy |
it decides what happens after the lookup's outcome |
| The amount, less any discount | the price step |
it is a rule of the business |
| Whether the card can be charged | the charge step |
it is what the business does with the order |
| Renewing an expired card | the rescue step, charge_rescue |
it repairs why the charge failed, so the charge can run once more |
| Giving up a charge that ended in the bank's error | the outage policy |
it decides what happens after an error the charge didn't handle |
| Writing every charge down | the audit policy |
it reacts to an outcome, and changes nothing in the charge |
| A gift card has nothing to ship | the ship step, which skips |
only the step knows its domain well enough |
| Shipping tried twice, with another carrier the second time | the workflow: a retry budget, and a provider for every attempt | how often to try, and with what, is the plan, not the rule |
| A parcel over the shop's limit | the ship step fails it, and the failedShipping policy gives it up before a retry |
the limit is the business's; giving up early is a decision after an outcome |
| Refunding a shipping given up | the failedShipping policy, through a role |
it reacts to a step given up, and acts on the outside world |
| Telling the customer | the notify policy, through a role |
it reacts to how the journey ended |
| The weight out of the warehouse's parcel | the workflow's input adapter | it changes the form of a step's data, and decides nothing |
How it works
Ask these questions about a new decision, in this order. The first answer that fits says where it belongs.
flowchart TD
accTitle: Where a decision belongs
accDescr: A decision that repairs why a step failed goes in a rescue step. Otherwise, a decision about what the business does goes in a step. Otherwise, how often a step is tried, and which services it gets, goes in the workflow. Anything else, deciding what happens after an outcome or acting on the outside world after one, goes in a policy's hook.
Q1{Does it repair why a step failed?} -->|yes| R[A rescue step]
Q1 -->|no| Q2{Is it what the business does?}
Q2 -->|yes| S[A step]
Q2 -->|no| Q3{Is it how often to try, or with what?}
Q3 -->|yes| W[The workflow]
Q3 -->|no| P[A policy's hook]The diagram asks three questions in turn, with four answers:
- A decision that repairs why a step failed, so that the step can run once more, belongs in a rescue step.
- Otherwise, a decision about what the business does, such as pricing, charging or shipping, belongs in a step.
- Otherwise, how often a step is tried, and which services it gets, belongs in the workflow: in its annotations, such as
retryBudget, and in its providers. - Otherwise, the decision says what happens after an outcome, or acts on the outside world after one, and belongs in a policy's hook.
Every decision in the table answers one of these questions; the adapter, which decides nothing, only changes the form of a step's data. The ID generator and the role implementations decide nothing about the flow either. Each place holds one kind of decision, and the journey records each one where it was taken: a step's outcome, a rescue step's repair, a retry the workflow allows, and the hook a policy ran.
But a rule put in the wrong place often still runs, and nothing in the shop's orders says so.
Outcomes
We have shown how to place a new decision of the shop.
| The usual way | With Itinera |
|---|---|
| Where a new rule goes is often decided by whoever writes it, next to the code they happen to be reading. | The workflow's declaration shows where each of the shop's decisions lives, one kind of decision to each place. |