Giving a step the tools it needs
What you want to do
The charge talks to the bank, and the shipping to the carrier. You want each step to ask for the tool it needs and use it, without knowing which bank or which carrier it is, while the workflow decides which one it gets.
The code
Each tool is an interface that says what the tool can do, and nothing more: a payment client that charges, a carrier that books and collects. These interfaces are all a step knows of its tools. An interface is gone once the program runs, so each also gets a token that stands for it while the program runs, made with serviceType, from itinera/service, and named for people.
/** A payment client: it charges an order's amount, and gives back a receipt. */
interface Payments {
charge(order: string, amount: number): string;
}
/**
* A carrier: it books a parcel and gives back its tracking number, then says
* whether it collects a parcel of this weight in kg.
*/
interface Carrier {
book(order: string): string;
collects(weight: number): boolean;
}
const PAYMENTS = serviceType<Payments>("Payments");
const CARRIER = serviceType<Carrier>("Carrier");The shop's bank and carrier are still faked, now as classes implementing each interface, and print what they do as before. The steps are those of chapter 6, with one change: each asks for its tool as a parameter of run marked @service(...), next to its contributor, and calls the tool instead of a function of its own.
/**
* Charges an order's price through the payment client, and gives back its
* receipt.
*/
@step()
class Charge extends Step {
constructor(
@input("order", string) private readonly order: string,
@input("price", integer) private readonly price: number,
) {
super();
}
override run(
@service("payments", PAYMENTS) payments: Payments,
@contributor() contributor: Contributor,
): Outcome {
if (this.price <= 0) return this.fail("nothing-to-charge");
const receipt = payments.charge(this.order, this.price);
contributor.contribute("receipt", receipt);
return this.succeed();
}
}
/**
* Ships an order through the carrier, with its receipt in the parcel, and
* gives back its tracking number.
*/
@step()
class Ship extends Step {
constructor(
@input("order", string) private readonly order: string,
@input("weight", integer) private readonly weight: number,
@input("receipt", string) private readonly receipt: string,
) {
super();
}
override run(
@service("carrier", CARRIER) carrier: Carrier,
@contributor() contributor: Contributor,
): Outcome {
const tracking = carrier.book(this.order);
contributor.contribute("tracking", tracking);
if (!carrier.collects(this.weight)) return this.fail("too-heavy");
console.log(`the parcel leaves with receipt ${this.receipt}`);
return this.succeed();
}
}The workflow lists the same steps, and its class gets a body: two methods marked @provider(...), each making one tool. Each prints when the shop gets its tool.
/**
* The shop's workflow: it charges, then ships, with the services each step
* needs.
*/
@workflow({ name: "orders" })
@withStep(Charge, { name: "charge" })
@withStep(Ship, { name: "ship" })
class Orders extends Workflow {
/** The bank, as the payment client of the journey. */
@provider(PAYMENTS, { key: "payments" })
bank(): Provided<Payments> {
console.log("the shop connects to the bank");
return this.provide(new Bank());
}
/** The main carrier, for the journey. */
@provider(CARRIER)
carrier(): Provided<Carrier> {
console.log("the shop calls the carrier");
return this.provide(new MainCarrier(20));
}
}The shop runs Ann's and Gil's orders as in chapter 6, and reads back the same output. It prints:
Ann's order:
the shop connects to the bank
the bank charges 40 for order A-1
the shop calls the carrier
the carrier books order A-1
the parcel leaves with receipt R-A-1
the journey succeeded, with receipt R-A-1 and tracking T-A-1
Gil's order:
the shop connects to the bank
the bank charges 45 for order A-7
the shop calls the carrier
the carrier books order A-7
the journey failed, with receipt R-A-7 and tracking noneHow it works
- A tool a step uses is a service. A payment client or a carrier is something a step uses to do its business, and in Itinera it is a service. It isn't a value: it never goes into the data bag. In TypeScript it is an object with methods, of the interface its token stands for.
- A step asks for a service by a key, and by its type. The parameter
@service("payments", PAYMENTS) payments: Paymentsasks for the service under the keypayments, and says it must be aPayments, by its token. The step asks for the interface, never for the shop's bank itself: it uses what it gets without knowing which implementation it is, or where it came from. - The workflow provides each service. For every key a step asks for, the workflow has a provider: a method of the workflow's class marked
@provider(...), which makes the service. The decorator names the service's token, and its key is the method's name, as forcarrier, unlesskeynames another, asbankprovidespayments. A provider returnsthis.provide(...)with the instance it made, a method thatWorkflowgives it, and its return type,Provided<Payments>, makes the compiler check that the instance is aPayments. A step decides what it does with a service; the workflow decides which service it gets. To charge through another bank, you change the provider, and no step. - A service is made once for each journey, the first time a step asks for it. The output shows when. In Ann's journey, the bank is connected just before her charge, and the carrier called just before her shipping. Gil's journey connects to the bank and calls the carrier again: every journey gets services of its own, and they are let go when it ends.
- The step has the service only for its attempt. The service is lent to the step for its attempt, as its contributor is. That is why it is a parameter of
run, and never of the constructor: the step's fields hold its data, and nothing it borrows. - What a service answers is the step's to handle. The carrier says it won't collect Gil's parcel of 60 kg, and the shipping turns that answer into its outcome, a failure with the reason
too-heavy, as it did in chapter 6. The executor never sees what a service answers, only the outcome the step reports.
So the steps no longer reach the bank or the carrier on their own, and the workflow says which ones they use. Now look at the output again. Every line of it comes from a console.log in the shop's own code: the steps, the faked tools and the providers print what they do, and the loop that runs the orders prints how each journey ended. What the executor did in between, such as when it ran each step and which step's failure ended Gil's journey, the shop sees only where its own code happens to print it.
Outcomes
We have shown how a step asks for the tools it needs, and how the workflow provides them.
| The usual way | With Itinera |
|---|---|
| A function often creates its clients, or is handed them by whoever builds it, so which bank an order uses is decided wherever that happens to be. | A step asks for a service by the interface it needs, and the workflow's provider decides which one it gets. |
| A client is often opened and closed by hand, around the calls that use it. | Each journey gets services of its own, made the first time a step asks and let go when the journey ends. |