ts-ioc-container 23.3.1 → 23.3.3

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.
Files changed (2) hide show
  1. package/README.md +37 -21
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -27,7 +27,7 @@ yarn add ts-ioc-container reflect-metadata
27
27
 
28
28
  ## Setup
29
29
  ### reflect-metadata
30
- Add it in main file of your project. It should be first line of code.
30
+ Just put it in the main file of your project. It should be the first line of the code.
31
31
  ```typescript
32
32
  import 'reflect-metadata';
33
33
  ```
@@ -42,11 +42,11 @@ import 'reflect-metadata';
42
42
  }
43
43
  ```
44
44
 
45
- ## Container
46
- Container consists of:
45
+ ## Container `IContainer`
46
+ It consists of 2 main parts:
47
47
 
48
- - Injector - injects dependencies to constructor
49
- - Providers - templates that create instances of dependencies
48
+ - Providers - describes how to create instances of dependencies
49
+ - Injector - describes how to inject dependencies to constructor
50
50
 
51
51
  ### Basic usage
52
52
 
@@ -70,8 +70,10 @@ container.dispose();
70
70
  ```
71
71
 
72
72
  ### Scopes
73
- - scope - is a container that can be created from another container. It's a sub-container.
74
- - NOTICE: when you create a scope of container then all providers are cloned to new scope. Every provider has method `clone` that clones itself.
73
+ Sometimes you need to create a scope of container. For example, when you want to create a scope per request in web application.
74
+
75
+ - NOTICE: remember that when you scope doesn't have dependency then it will be resolved from parent container
76
+ - NOTICE: when you create a scope of container then all providers are cloned to new scope. For that reason every provider has methods `clone` and `isValid` to clone itself and check if it's valid for certain scope accordingly.
75
77
 
76
78
  ```typescript
77
79
  import { Container, ReflectionInjector } from "ts-ioc-container";
@@ -81,10 +83,11 @@ const scope = container.createScope();
81
83
  ```
82
84
 
83
85
  ### Tags
86
+ Sometimes you want to mark some providers and resolve them only from certain scope. So you can assign tags to providers and create scopes with certain tags. For that reason every scope has method `hasTag` which we invoke from provider to check if it's valid for certain scope.
84
87
 
85
- - tag - is a string that can be used mark each container and scope
88
+ - tag - is a string that we assign to provider and scope/container
86
89
  - every provider can be registered per certain tags and cannot be resolved from container with other tags. Only from parent one with certain tags.
87
- - NOTICE: when you create a scope of container with tags which not includes in provider tags then this provider will not be cloned to new scope.
90
+ - NOTICE: when you create a scope then we clone ONLY tags-matched providers.
88
91
 
89
92
  ```typescript
90
93
  import { Container, perTags, ReflectionInjector } from "ts-ioc-container";
@@ -96,6 +99,8 @@ scope.resolve('ILogger'); // it will be resolved from container, not from scope
96
99
  ```
97
100
 
98
101
  ### Instances
102
+ Sometimes you want to get all instances from container and its scopes. For example, when you want to dispose all instances of container.
103
+
99
104
  - you can get instances from container and scope which were created by injector
100
105
 
101
106
  ```typescript
@@ -112,6 +117,8 @@ expect(container.getInstances().length).toBe(2);
112
117
  ```
113
118
 
114
119
  ### Disposing
120
+ Sometimes you want to dispose container and all its scopes. For example, when you want to prevent memory leaks. Or you want to ensure that nobody can use container after it was disposed.
121
+
115
122
  - container can be disposed
116
123
  - when container is disposed then all scopes are disposed too
117
124
  - when container is disposed then it unregisters all providers and remove all instances
@@ -130,12 +137,15 @@ expect(() => container.resolve('ILogger')).toThrow(ContainerDisposedError);
130
137
  expect(container.getInstances().length).toBe(0);
131
138
  ```
132
139
 
133
- ## Injectors
140
+ ## Injectors `IInjector`
141
+ Injectors are used to describe how dependencies should be injected to constructor.
142
+
134
143
  - `ReflectionInjector` - injects dependencies using `@inject` decorator
135
144
  - `ProxyInjector` - injects dependencies as dictionary `Record<string, unknown>`
136
145
  - `SimpleInjector` - just passes container to constructor with others arguments
137
146
 
138
147
  ### Reflection injector
148
+ This type of injector uses `@inject` decorator to mark where dependencies should be injected. It's bases on `reflect-metadata` package. That's why I call it `ReflectionInjector`.
139
149
 
140
150
  ```typescript
141
151
  import { Container, IContainer, IInjector, Provider, by, inject, resolve } from "ts-ioc-container";
@@ -167,6 +177,7 @@ app.run();
167
177
  ```
168
178
 
169
179
  ### Simple injector
180
+ This type of injector just passes container to constructor with others arguments.
170
181
 
171
182
  ```typescript
172
183
  import { SimpleInjector, IContainer } from "ts-ioc-container";
@@ -197,6 +208,7 @@ app.run();
197
208
  ```
198
209
 
199
210
  ### Proxy injector
211
+ This type of injector injects dependencies as dictionary `Record<string, unknown>`.
200
212
 
201
213
  ```typescript
202
214
  import { ProxyInjector, IContainer } from "ts-ioc-container";
@@ -226,9 +238,13 @@ const app = container.resolve(App);
226
238
  app.run();
227
239
  ```
228
240
 
229
- ## Providers
230
- All providers are registered in container and cloned for every sub-scope.
241
+ ## Providers `IProvider<T>`
242
+ Providers are used to describe how instances should be created. It has next basic methods:
243
+ - `resolve` - creates instance with passed arguments
244
+ - `clone` - we invoke it when we create a scope. It clones provider to new scope.
245
+ - `isValid` - checks if provider can be resolved from container or cloned to container with certain tags
231
246
 
247
+ There are next types of providers:
232
248
  - `Provider` - basic provider
233
249
  - `SingletonProvider` - provider that creates only one instance in every scope where it's resolved
234
250
  - `TaggedProvider` - provider that can be resolved only from container with certain tags and their sub scopes
@@ -279,6 +295,7 @@ container.register('ILogger', Provider.fromClass(Logger));
279
295
  ```
280
296
 
281
297
  ### Singleton provider
298
+ Sometimes you need to create only one instance of dependency per scope. For example, you want to create only one logger per scope.
282
299
 
283
300
  - Singleton provider creates only one instance in every scope where it's resolved.
284
301
  - NOTICE: if you create a scope 'A' of container 'root' then Logger of A !== Logger of root.
@@ -298,8 +315,8 @@ container.resolve('ILogger') !== scope.resolve('ILogger'); // true. NOTICE: beca
298
315
  ```
299
316
 
300
317
  ### Tagged provider
301
- You need tagged provider when you want to resolve provider only from container with certain tags and their sub scopes.
302
- It doesn't make a clones in scopes with tags that are not in provider's tags. Usually it's used with `SingletonProvider`.
318
+ Sometimes you need to resolve provider only from container with certain tags and their sub scopes. Especially if you want to register dependency as singleton for some tags, for example `root`
319
+ - NOTICE: It doesn't make clones in not tagged-matched scopes. Usually it's used with `SingletonProvider`.
303
320
 
304
321
  ```typescript
305
322
  import { Provider, TaggedProvider, asSingleton, perTags } from "ts-ioc-container";
@@ -318,7 +335,7 @@ container.resolve('ILogger') === scope.resolve('ILogger'); // true
318
335
  ```
319
336
 
320
337
  ### Args provider
321
- - You need args provider when you want to pass arguments to constructor on step when you compose container.
338
+ Sometimes you want to bind some arguments to provider. This is what `ArgsProvider` is for.
322
339
  - NOTICE: args from this provider has higher priority than args from `resolve` method.
323
340
 
324
341
  ```typescript
@@ -341,8 +358,7 @@ container.resolve('ILogger', 'Main').name === 'Main'; // true
341
358
  ```
342
359
 
343
360
  ## Container modules
344
-
345
- if you want to encapsulate some logic to enrich container you can use `IContainerModule`.
361
+ Sometimes you want to encapsulate registration logic in separate module. This is what `IContainerModule` is for.
346
362
 
347
363
  ```typescript
348
364
  import { Registration } from "ts-ioc-container";
@@ -365,7 +381,7 @@ const container = new Container(injector, { tags: ['root'] })
365
381
  ```
366
382
 
367
383
  ## Registration module (Provider + DependencyKey)
368
- It's built-in module that encapsulates logic of registration provider by dependency key `forKey`. Just a sugar
384
+ Sometimes you need to keep dependency key with class together. For example, you want to register class with key 'ILogger' and you want to keep this key with class. This is what `Registration` is for.
369
385
 
370
386
  ```typescript
371
387
  import { asSingleton, perTags, forKey, Registration, Provider } from "ts-ioc-container";
@@ -393,7 +409,7 @@ container.register('ILogger', Provider.fromClass(Logger));
393
409
  ```
394
410
 
395
411
  ## Hooks
396
- You can mark methods of your classes as hooks. It's useful when you want to do something after construct of dispose classes.
412
+ Sometimes you need to invoke methods after construct or dispose of class. This is what hooks are for.
397
413
 
398
414
  ```typescript
399
415
  import {
@@ -444,8 +460,8 @@ for (const instance of container.getInstances()) {
444
460
  }
445
461
  ```
446
462
 
447
- ## Mocking / Tests
448
- `AutoMockedContainer`. It will generate mocks for every dependency that you didn't define.
463
+ ## Mocking / Tests `AutoMockedContainer`
464
+ Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
449
465
 
450
466
  ```typescript
451
467
  import {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-ioc-container",
3
- "version": "23.3.1",
3
+ "version": "23.3.3",
4
4
  "description": "Typescript IoC container",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -52,5 +52,5 @@
52
52
  "ts-jest": "27.0.5",
53
53
  "typescript": "4.4.3"
54
54
  },
55
- "gitHead": "4537cec8aa8d750a5aafbc5179bf1ec6d15b0102"
55
+ "gitHead": "1e371026880aaf0022c15baadb7d8c3411f7a217"
56
56
  }