Projects / DI container / Wrap up
Wrap up
Keep going
- Async factories. Let a factory return a promise (for example a database connection) and add
resolveAsync. - Child overrides. Let a scope register its own replacement for one token, such as a per-request fake.
- Modules. Group related registrations into a function
(container) => voidand compose them. - Lazy injection. Pass a function that resolves later, to break a real cycle on purpose.
- Better errors. Suggest the closest registered token name when one is missing.
- Symbol.dispose. Support the standard disposal protocol instead of a method named
dispose.
What this version leaves out, and what a real container does
- Many containers use decorators and runtime type information to read constructor types. We chose explicit tokens, which cost more typing and need no build-time magic.
- Real containers support async construction, named registrations, multiple values for one token and child containers that override parents.
- Our
disposeonly tracks singletons and scoped services that have a method nameddispose. Real libraries track more and support async cleanup. - Our scopes are not safe against a single factory running while another resolve starts on the same root in the middle (the shared path list assumes synchronous factories). Async code needs a different design for that list.
Check yourself
Answer each question in your head first, then open it to check.
Why does
Token<T>have a field that is never assigned?It makesToken<string>andToken<number>different types. Without a field that mentionsT, the compiler sees identical classes and stops checking that factories match their tokens.What is the difference between singleton, scoped and transient, and where is each one cached?
A singleton is built once and cached in the container that registered it. A scoped service is built once per scope and cached in that scope. A transient is built every time and not cached.Why does a singleton factory get called with the container that registered it, not the scope that asked?
So a singleton cannot reach per-request services. If it could, it would keep the first request's objects forever (a captive dependency). The root container refuses to build scoped services, so the mistake is an error the first time it runs.In
Values, what doesconst Dbuy you at the call site?It keeps the list you wrote as a fixed-length tuple of specific tokens instead of an array. The compiler then knows how many values the function receives and the type of each, in order.Teach it back: a teammate wants to call
container.resolve(...)inside every service class "for convenience". What do you tell them?Resolving inside services hides what they need and ties them to the container, which makes them hard to test and to move. Resolve at the edges (startup, start of a request) and pass plain values into constructors.
Sources
- TypeScript: types (tsconfig), what the
typescompiler option includes. - Node.js: test runner, the
node:testmodule used in Step 4.3. - Node.js: Packages, how
exportsdefines a package's entry points.