@adimm/x-injection 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,11 @@ xInjection&nbsp;<a href="https://www.npmjs.com/package/@adimm/x-injection" targe
22
22
  - [Getting Started](#getting-started)
23
23
  - [Bootstrapping the AppModule](#bootstrapping-the-appmodule)
24
24
  - [Registering Global Providers](#registering-global-providers)
25
+ - [Registering Global Modules](#registering-global-modules)
25
26
  - [Injection Scope](#injection-scope)
27
+ - [Singleton](#singleton)
28
+ - [Transient](#transient)
29
+ - [Request](#request)
26
30
  - [Custom Provider Modules](#custom-provider-modules)
27
31
  - [Dynamic Exports](#dynamic-exports)
28
32
  - [Advanced Usage](#advanced-usage)
@@ -108,6 +112,29 @@ AppModule.register({
108
112
 
109
113
  Now, `LoggerService` and `ConfigService` can be injected anywhere in your app, including inside all `ProviderModules`.
110
114
 
115
+ ### Registering Global Modules
116
+
117
+ You can also import entire modules into the `AppModule` like so:
118
+
119
+ ```ts
120
+ const ConfigModule = new ProviderModule({
121
+ identifier: Symbol('ConfigModule'),
122
+ markAsGlobal: true,
123
+ providers: [
124
+ { provide: 'SECRET_TOKEN', useValue: '123' },
125
+ { provide: 'SECRET_TOKEN_2', useValue: 123 },
126
+ ],
127
+ });
128
+
129
+ AppModule.register({
130
+ imports: [ConfigModule],
131
+ });
132
+ ```
133
+
134
+ > **Note:** _All modules which are imported into the `AppModule` must have the [markAsGlobal](https://adimarianmutu.github.io/x-injection/interfaces/ProviderModuleOptions.html#markAsGlobal) option set to `true`, otherwise the [InjectionProviderModuleGlobalMarkError](https://adimarianmutu.github.io/x-injection/classes/InjectionProviderModuleGlobalMarkError.html) exception will be thrown!_
135
+ >
136
+ > **Note2:** _An [InjectionProviderModuleGlobalMarkError](https://adimarianmutu.github.io/x-injection/classes/InjectionProviderModuleGlobalMarkError.html) exception will be thrown also when importing into the `AppModule` a module which does **not** have the [markAsGlobal](https://adimarianmutu.github.io/x-injection/interfaces/ProviderModuleOptions.html#markAsGlobal) flag option!_
137
+
111
138
  ### Injection Scope
112
139
 
113
140
  There are mainly 3 first-class ways to set the [InjectionScope](https://adimarianmutu.github.io/x-injection/enums/InjectionScope.html) of a provider, and each one has an order priority.
@@ -136,6 +163,54 @@ The below list shows them in order of priority _(highest to lowest)_, meaning th
136
163
 
137
164
  > **Note:** _Imported modules/providers retain their original `InjectionScope`!_
138
165
 
166
+ #### Singleton
167
+
168
+ The [Singleton](https://adimarianmutu.github.io/x-injection/enums/InjectionScope.html#singleton) injection scope means that once a dependency has been resolved from within a module will be cached and further resolutions will use the value from the cache.
169
+
170
+ Example:
171
+
172
+ ```ts
173
+ expect(MyModule.get(MyProvider)).toBe(MyModule.get(MyProvider));
174
+ // true
175
+ ```
176
+
177
+ #### Transient
178
+
179
+ The [Singleton](https://adimarianmutu.github.io/x-injection/enums/InjectionScope.html#transient) injection scope means that a _new_ instance of the dependency will be used whenever a resolution occurs.
180
+
181
+ Example:
182
+
183
+ ```ts
184
+ expect(MyModule.get(MyProvider)).toBe(MyModule.get(MyProvider));
185
+ // false
186
+ ```
187
+
188
+ #### Request
189
+
190
+ The [Request](https://adimarianmutu.github.io/x-injection/enums/InjectionScope.html#request) injection scope means that the _same_ instance will be used when a resolution happens in the _same_ request scope.
191
+
192
+ Example:
193
+
194
+ ```ts
195
+ class Book {}
196
+
197
+ class Box {
198
+ constructor(
199
+ private readonly book0: Book,
200
+ private readonly book1: Book
201
+ ) {}
202
+ }
203
+
204
+ const box0 = MyModule.get(Box);
205
+ const box1 = MyModule.get(Box);
206
+
207
+ expect(box0.book0).toBe(box0.book1);
208
+ // true
209
+
210
+ expect(box0.book0).toBe(box1.book0);
211
+ // false
212
+ ```
213
+
139
214
  ## Custom Provider Modules
140
215
 
141
216
  You can define custom modules to encapsulate related providers and manage their scope: