UNPKG

3.37 kBMarkdownView Raw
1# @loopback/context
2
3This module provides facilities to manage artifacts and their dependencies using
4`Context` in your Node.js applications. It can be used independent of the
5LoopBack framework.
6
7## Overview
8
9The `@loopback/context` package exposes TypeScript/JavaScript APIs and
10decorators to register artifacts, declare dependencies, and resolve artifacts by
11keys. The `Context` also serves as an
12[IoC container](https://en.wikipedia.org/wiki/Inversion_of_control) to support
13[dependency injection](https://en.wikipedia.org/wiki/Dependency_injection).
14
15`Context` and `Binding` are the two core concepts. A context is a registry of
16bindings and each binding represents a resolvable artifact by the key.
17
18- Bindings can be fulfilled by a constant, a factory function, a class, or a
19 provider.
20- Bindings can be grouped by tags and searched by tags.
21- Binding scopes can be used to control how a resolved binding value is shared.
22- Bindings can be resolved synchronously or asynchronously.
23- Provide `@inject` and other variants of decorators to express dependencies.
24- Support Constructor, property, and method injections.
25- Allow contexts to form a hierarchy to share or override bindings.
26- Track bindings and injections during resolution to detect circular
27 dependencies.
28
29## Installation
30
31```sh
32npm install --save @loopback/context
33```
34
35## Basic use
36
37### Locate an artifact by key
38
39```js
40const Context = require('@loopback/context').Context;
41const ctx = new Context();
42ctx.bind('hello').to('world'); // BindingKey='hello', BindingValue='world'
43const helloVal = ctx.getSync('hello');
44console.log(helloVal); // => 'world'
45```
46
47The binding can also be located asynchronously:
48
49```ts
50const helloVal = await ctx.get('hello');
51console.log(helloVal); // => 'world'
52```
53
54### Dependency injection using context
55
56```ts
57import {Context, inject} from '@loopback/context';
58const ctx = new Context();
59
60// bind 'greeting' to 'Hello' as the constant value
61ctx.bind('greeting').to('Hello');
62
63class HelloController {
64 constructor(
65 // injecting the value bound to `greeting` using context
66 @inject('greeting') private greeting: string,
67 ) {}
68
69 greet(name) {
70 return `${this.greeting}, ${name}`;
71 }
72}
73
74// Bind 'HelloController' to class HelloController
75ctx.bind('HelloController').toClass(HelloController);
76
77async function hello() {
78 // Get an instance of HelloController
79 const helloController = await ctx.get<HelloController>('HelloController');
80 // helloController now has the `greeting` property injected with `Hello`
81 console.log(helloController.greet('John')); // => Hello, John
82}
83
84hello();
85```
86
87For additional information, please refer to the
88[Context page](http://loopback.io/doc/en/lb4/Context.html).
89
90## Examples
91
92To learn more about advanced features, check out standalone examples at
93[`@loopback/example-context`](https://github.com/loopbackio/loopback-next/tree/master/examples/context).
94
95Use the following command to download the example project to try out:
96
97```sh
98lb4 example context
99```
100
101## Contributions
102
103- [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
104- [Join the team](https://github.com/loopbackio/loopback-next/issues/110)
105
106## Tests
107
108Run `npm test` from the root folder.
109
110## Contributors
111
112See
113[all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
114
115## License
116
117MIT