@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

main
md 67 lines 4.65 KB
Rendered Raw
1 # Breaking requirements into building blocks
2
3 Work from the language of the business. Write the requirements as sentences, then mark each one.
4
5 ## 1. List the facts
6
7 Every past-tense statement the business cares about is an event. "The customer withdrew money" becomes `MoneyWithdrawn { amount, balanceAfter }`. An event carries what changed and what a reader needs (here `balanceAfter`, so views don't recompute). Name it from the business's point of view, never as a CRUD verb (`AccountUpdated` is a smell).
8
9 More: [Event](https://alchemy.run/domain/event).
10
11 ## 2. Find the decisions
12
13 Each event is produced by a command that someone asks for: `WithdrawMoney { amount }`. Ask what can refuse it. Each answer is a rejection carrying the evidence (`InsufficientFunds { balance, requested }`, `DailyLimitExceeded { remaining }`). Name commands `<Verb><Noun>`.
14
15 More: [Command](https://alchemy.run/domain/command).
16
17 ## 3. Draw consistency boundaries
18
19 Group the commands by what must be checked together in one transaction. That group is an aggregate, keyed by an id (`Account` keyed by `AccountId`). Its state holds only what its decisions need. Keep aggregates small: one instance is one writer, and there are no transactions across instances.
20
21 When a rule spans instances, pick one of these:
22
23 - **Invariant-shaped aggregate.** Uniqueness of a username across customers becomes `UsernameClaim`, keyed by the username. Two claims race on one instance and exactly one wins.
24 - **Reserve, then commit.** A policy claims first (`ClaimUsername`) and only then confirms (`ConfirmRegistration`), or rejects (`RejectRegistration`).
25 - **Deadline.** A process that waits for a decision schedules a command that can't be rejected (`ExpireTransfer`), on the same aggregate as the decision, so exactly one of approve and expire wins.
26
27 More: [Aggregate](https://alchemy.run/domain/aggregate), [Domain Model: Each command goes to one instance](https://alchemy.run/domain/model#each-command-goes-to-one-instance).
28
29 ## 4. Answer the questions
30
31 Every screen or report is a view, keyed by what it's about: `AccountSummary` keyed by account, `CustomerDashboard` keyed by customer. A view folds events from one or more aggregates (or other views) into state. It can also emit its own events (`Statement` turns six money events into `StatementLine`). Route one event to several keys with named routes in `keyOf`, e.g. a transfer shows on both accounts.
32
33 A view is a security boundary. Its `validate` judges every read from who is asking. Make a separate view per audience instead of filtering by caller.
34
35 More: [View](https://alchemy.run/domain/view).
36
37 ## 5. Find the reactions
38
39 "Whenever X, do Y" is a policy, listening to an aggregate or a view. "When a customer registers, open their main account" is `MainAccountProvisioning` on `CustomerRegistered`, sending `OpenAccount`. A policy has no session, so it provides the request context its commands need (e.g. `CurrentActor` = system).
40
41 More: [Policy](https://alchemy.run/domain/policy).
42
43 ## 6. Find the processes
44
45 A reaction with several steps, waits, or undo is a saga: an Effect workflow keyed by the aggregate it's about. "Move money: debit, credit; if the credit fails, refund; large transfers wait for approval" is `ExecuteTransfer`. A policy starts it on `TransferRequested` and signals it on `TransferApproved`. Each step is an `Activity` that sends one command. `withCompensation` undoes a step, and `DurableDeferred` waits for a signal.
46
47 More: [Saga](https://alchemy.run/domain/saga).
48
49 ## 7. Find what's outside
50
51 Calls to other systems are external systems: typed operations declared in the domain and implemented in the server (`FraudCheck.score`, `Payments.refund`). Writes to stores outside the model are projections (`Ledger` writes rows through a `LedgerStore` port). A per-key log that clients read is a stream (`AccountCard`), written by a projection.
52
53 More: [External System](https://alchemy.run/domain/external-system).
54
55 ## 8. Decide who sees what
56
57 Group commands and reads into Apis by audience (`CustomerApi`, `SupportApi`). Middleware authenticates (`Session` provides `CurrentActor`; `MfaChallenge` guards sensitive commands). Each command's `decide` and each read's `validate` authorizes against the current state.
58
59 More: [Views as security boundaries](https://alchemy.run/domain/view-security).
60
61 ## Smells
62
63 - A command that touches two aggregates. Split it, and connect the parts with a policy or a saga.
64 - A view that filters by caller. Make one view per audience.
65 - A policy that waits for a result. Use a saga, and react to the events it records.
66 - An event named after a field change (`BalanceSet`). Name the business fact instead.
67 - A rejection without evidence. Add the fields a client needs to explain it.