1 | # @loopback/context
|
2 |
|
3 | This module provides facilities to manage artifacts and their dependencies using
|
4 | `Context` in your Node.js applications. It can be used independent of the
|
5 | LoopBack framework.
|
6 |
|
7 | ## Overview
|
8 |
|
9 | The `@loopback/context` package exposes TypeScript/JavaScript APIs and
|
10 | decorators to register artifacts, declare dependencies, and resolve artifacts by
|
11 | keys. 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
|
16 | bindings 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
|
32 | npm install --save @loopback/context
|
33 | ```
|
34 |
|
35 | ## Basic use
|
36 |
|
37 | ### Locate an artifact by key
|
38 |
|
39 | ```js
|
40 | const Context = require('@loopback/context').Context;
|
41 | const ctx = new Context();
|
42 | ctx.bind('hello').to('world'); // BindingKey='hello', BindingValue='world'
|
43 | const helloVal = ctx.getSync('hello');
|
44 | console.log(helloVal); // => 'world'
|
45 | ```
|
46 |
|
47 | The binding can also be located asynchronously:
|
48 |
|
49 | ```ts
|
50 | const helloVal = await ctx.get('hello');
|
51 | console.log(helloVal); // => 'world'
|
52 | ```
|
53 |
|
54 | ### Dependency injection using context
|
55 |
|
56 | ```ts
|
57 | import {Context, inject} from '@loopback/context';
|
58 | const ctx = new Context();
|
59 |
|
60 | // bind 'greeting' to 'Hello' as the constant value
|
61 | ctx.bind('greeting').to('Hello');
|
62 |
|
63 | class 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
|
75 | ctx.bind('HelloController').toClass(HelloController);
|
76 |
|
77 | async 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 |
|
84 | hello();
|
85 | ```
|
86 |
|
87 | For additional information, please refer to the
|
88 | [Context page](http://loopback.io/doc/en/lb4/Context.html).
|
89 |
|
90 | ## Examples
|
91 |
|
92 | To 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 |
|
95 | Use the following command to download the example project to try out:
|
96 |
|
97 | ```sh
|
98 | lb4 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 |
|
108 | Run `npm test` from the root folder.
|
109 |
|
110 | ## Contributors
|
111 |
|
112 | See
|
113 | [all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
|
114 |
|
115 | ## License
|
116 |
|
117 | MIT
|