Lesson 4 of 7 · 3 min read
Dependencies and application boundaries
A course notification passes through an HTTP handler, a scheduling policy, a repository and a delivery provider. Follow what each module knows as the message moves between them.
Separate injection from inversion
A worker that receives send in its constructor or factory uses dependency injection. It can still depend on the provider if send has a vendor SDK type. Inversion happens when the scheduling or delivery use case owns a contract written in its own terms and the external adapter imports it. The worker can call the adapter at runtime while source dependencies still point toward the application contract. Test the policy by supplying a local implementation with the same result semantics.
Wire concrete resources at startup
In the composition root, read validated configuration, open the database client, construct the repository and delivery adapter, and give them to the worker. The entry point that starts the worker owns cleanup of resources it opens. Explicit wiring makes it possible to see which clients are shared and which values, such as a request or job identity, belong to one call. A process-local instance does not coordinate a fleet. Introduce a container only if it improves the application's construction and lifetime handling.
Give storage operations domain meaning
A repository might expose createPending(plan) and claimDue(now, limit). Define what a claim returns if another worker already took the plan. Implement the claim atomically in the database adapter. Specify transaction ownership when one use case must write through several collaborators, and verify concurrency behavior against a real database. A persistence mapper converts a stored row to a plan and prepares values for storage. Recover the existing identity and status without replaying the effects of initially creating the plan.
Keep the wire contract distinct
Parse the HTTP input with the shared runtime schema before calling the use case. Project its result into a response DTO containing a public plan ID and status, without passing through provider handles or internal rejection notes. TypeScript types alone do not filter fields at runtime. If a table renames scheduled_at to due_at, update the persistence adapter and keep the HTTP field dueAt stable. A read-only history query can select a DTO directly when it has no domain behavior to execute.
The use case owns its capabilities, startup chooses concrete implementations, and each boundary translates only the data it needs.
function makeReminderService({ clock, subscriptions, sender }) {
return {
async run(courseId) {
const eligible = await subscriptions.findDue(courseId, clock.now());
for (const recipient of eligible) {
await sender.deliver({ courseId, recipient });
}
},
};
}