Menu

Dependency Injection in KickJS — Tokens, Scopes, and the Per-Request Factory
📰

Dependency Injection in KickJS — Tokens, Scopes, and the Per-Request Factory

DEV Community·Orinda Felix Ochieng·6 months ago
#74ie8edn
#kickjsl#typescript#dependency#request#scope
Reading 0:00
15s threshold

Dependency injection in KickJS is small on purpose. There are four surfaces, two scopes, and one pattern — a per-request factory — that does most of the interesting work. Once you know how those pieces compose, you can wire anything from a process-wide HTTP client to a request-scoped derived value through the same container, and your services keep looking like the boring constructor-and-method classes you wanted them to be in the first place.

This article walks the four DI entry points, explains when to use a token versus a class, draws the line between singleton and request scope, and then shows the one pattern that ties it together: an adapter that registers a request-scoped factory which reads request state and returns a derived handle.

The four DI surfaces

KickJS exposes DI through four distinct entry points. Each suits a different role, and a typical class will use two or three of them at once.

1. @Service() — auto-registered class. Decorate a class and the container learns to construct it.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.