Introduction
Framework Agnostic, Zero Dependency, Isomorphic & Minimalistic Dependency Injection Container for TypeScript and JavaScript projects
Last updated
Was this helpful?
Framework Agnostic, Zero Dependency, Isomorphic & Minimalistic Dependency Injection Container for TypeScript and JavaScript projects
Last updated
Was this helpful?
true-di
is designed to be used with considering to make resolvers as thin as possible, to keep all business logic testable and framework agnostic by strictly following S.O.L.I.D Principles.
Despite the origin motivation being related to Apollo Server the library could be used with any other framework or library that supports injection through the context.
The shows how to use true-di
with one of the most popular nodejs libraries: . Almost all code in the example (~95%) is covered with tests that prove your business logic could be easily decoupled and kept independent even from dependency injection mechanism.
Thanking to business logic does not depend on specific injection mechanism you can defer the choice of framework. Such deferring is suggested by Robert Martin:
The purpose of a good architecture is to defer decisions, delay decisions. The job of an architect is not to make decisions, the job of an architect is to build a structure that allows decisions to be delayed as long as possible.
,
Summarizing, true-di
is based on:
emulattion of a plain object that allows to specify exact type for each item and makes strict static type checking possible
explicit dependency injection for business logic (see example: )
transperent injection through the context for framework-integrated components (see example: )
true-di
?No syntatic decorators (annotations) as well as reflect-metadata
are needed
diContainer
uses getters
under the hood. Container doesn't create an item until your code requests
Constructor-injection of cyclic dependency raises an exception
The property-, setter- dependency injects is also supported (what allows to resolve cyclic dependencies)
diContainer
-s could be composed
Symbolic names are also supported for items