Read the container, know the graph
When you open a new codebase, one of the first things you want to know is how it fits together: what talks to what, and what needs what. In a snapfit application, that answer is written in one struct. Let's see how much one container tells you, and why you can write its fields in whatever order reads best.
Five components, written top-down
Our checkout now has five components across four layers: a configuration, a database and a payment client, an order repository, and the checkout handler at the top. We write the container the way you would explain it to a colleague, starting from the entry point and ending with the configuration:
#[container]
pub struct ShopContainer {
#[singleton(deps = [repo, payments])]
checkout: Arc<CheckoutHandler>,
#[singleton(deps = [db])]
repo: Arc<OrderRepository>,
#[singleton(deps = [config])]
payments: Arc<PaymentClient>,
#[singleton(deps = [config])]
db: Arc<Database>,
#[singleton]
config: Arc<ShopConfig>,
}That's the opposite of the order they have to be built in. Let's run it anyway:
fn main() {
let container = ShopContainer::new();
let checkout = container.get_checkout();
println!("{}", checkout.handle(1042, 2999));
}It prints:
charged 2999 cents in eu-west | saved order #1042 in eu-westIt just works. Think of a recipe whose steps each say what they need. You can write the steps in any order: the baking step says it needs the batter, so the batter is mixed first.
The graph, read from the struct
Here is everything the container says, drawn as a picture:
flowchart TD
accTitle: Compile-time dependency graph for ShopContainer
accDescr: ShopConfig is a leaf singleton at the root. Database and PaymentClient both depend on ShopConfig. OrderRepository depends on Database. CheckoutHandler depends on OrderRepository and PaymentClient.
CFG["config: Arc<ShopConfig> (singleton)"] --> DB["db: Arc<Database> (singleton)"]
CFG --> PAY["payments: Arc<PaymentClient> (singleton)"]
DB --> REPO["repo: Arc<OrderRepository> (singleton)"]
REPO --> CHK["checkout: Arc<CheckoutHandler> (singleton)"]
PAY --> CHKWhat the diagram shows:
config(Arc<ShopConfig>) needs nothing.db(Arc<Database>) andpayments(Arc<PaymentClient>) both need onlyconfig.repo(Arc<OrderRepository>) needsdb.checkout(Arc<CheckoutHandler>) needsrepoandpayments, and receives them in the order itsdepslists them.
You didn't have to open a single service to draw that. Everything came from ShopContainer.
How the container finds the order
snapfit visits the fields in the order you declared them. Before it builds a field, it builds the fields that one needs, in the order its deps lists them, and the fields those need in turn. Let's follow it:
checkoutcomes first, but it needsrepoandpayments, so it waits.reponeedsdb, anddbneedsconfig. Soconfigis built first, thendb, thenrepo.paymentsneeds onlyconfig, which is already there, so it is built next.- With
repoandpaymentsready,checkoutis built last.
All of this is worked out when the program compiles, so it costs nothing when it runs, and it is the same every time.
Which problem this solves
- Ordering by hand (Chapter 3): the fields are written top-down, the opposite of the order they must be built in, and
new()still builds each one after the fields it needs. - An invisible graph (Chapter 3): every component and every arrow of the diagram above is written in
ShopContainer, and nowhere else.
Remember
- Declare the fields of a
#[container]in whatever order reads best: each one is built after the fields it needs. - The order of names inside
deps = [repo, payments]does matter: it must match the order of the constructor's parameters,CheckoutHandler::new(repo, payments). - The
#[container]struct alone tells you every component and what it needs, without opening any other file.