This module provides facilities to manage artifacts and their dependencies using
Context in your Node.js applications. It can be used independent of the
decorators to register artifacts, declare dependencies, and resolve artifacts by
Context also serves as an
IoC container to support
Binding are the two core concepts. A context is a registry of
bindings and each binding represents a resolvable artifact by the key.
- Bindings can be fulfilled by a constant, a factory function, a class, or a provider.
- Bindings can be grouped by tags and searched by tags.
- Binding scopes can be used to control how a resolved binding value is shared.
- Bindings can be resolved synchronously or asynchronously.
@injectand other variants of decorators to express dependencies.
- Support Constructor, property, and method injections.
- Allow contexts to form a hierarchy to share or override bindings.
- Track bindings and injections during resolution to detect circular dependencies.
npm install --save @loopback/context
Locate an artifact by key
const Context = Context;const ctx = ;ctx; // BindingKey='hello', BindingValue='world'const helloVal = ctx;console; // => 'world'
The binding can also be located asynchronously:
;console.loghelloVal; // => 'world'
Dependency injection using context
;;// bind 'greeting' to 'Hello' as the constant valuectx.bind'greeting'.to'Hello';// Bind 'HelloController' to class HelloControllerctx.bind'HelloController'.toClassHelloController;hello;
For additional information, please refer to the Context page.
To learn more about advanced features, check out standalone examples at
Use the following command to download the example project to try out:
lb4 example context
npm test from the root folder.
See all contributors.