ts-ioc-container 25.0.2 → 25.0.4
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 +56 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
-

|
|
6
6
|
[](https://coveralls.io/github/IgorBabkin/ts-ioc-container?branch=master)
|
|
7
7
|

|
|
8
8
|
|
|
9
|
+
* * *
|
|
10
|
+
|
|
9
11
|
## Advantages
|
|
10
12
|
- battle tested :boom:
|
|
11
13
|
- written on `typescript`
|
|
@@ -17,7 +19,34 @@
|
|
|
17
19
|
- composable and open to extend
|
|
18
20
|
- awesome for testing (auto mocking)
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
* * *
|
|
23
|
+
|
|
24
|
+
- [Setup](#setup)
|
|
25
|
+
- [Container](#container)
|
|
26
|
+
- [Basic usage](#basic-usage)
|
|
27
|
+
- [Scopes](#scopes)
|
|
28
|
+
- [Instances](#instances)
|
|
29
|
+
- [Disposing](#disposing)
|
|
30
|
+
- [Injectors](#injectors)
|
|
31
|
+
- [Reflection injector](#reflection-injector)
|
|
32
|
+
- [Simple injector](#simple-injector)
|
|
33
|
+
- [Proxy injector](#proxy-injector)
|
|
34
|
+
- [Providers](#providers)
|
|
35
|
+
- [Provider](#provider)
|
|
36
|
+
- [Singleton provider](#singleton-provider)
|
|
37
|
+
- [Tagged provider](#tagged-provider)
|
|
38
|
+
- [Args provider](#args-provider)
|
|
39
|
+
- [Container modules](#container-modules)
|
|
40
|
+
- [Basic usage](#basic-usage-1)
|
|
41
|
+
- [Registration module (Provider + DependencyKey)](#registration-module-provider--dependencykey)
|
|
42
|
+
- [Hooks](#hooks)
|
|
43
|
+
- [Tests and Mocks](#tests-and-mocks)
|
|
44
|
+
- [Errors](#errors)
|
|
45
|
+
|
|
46
|
+
* * *
|
|
47
|
+
|
|
48
|
+
## Setup
|
|
49
|
+
|
|
21
50
|
```shell script
|
|
22
51
|
npm install ts-ioc-container reflect-metadata
|
|
23
52
|
```
|
|
@@ -25,14 +54,12 @@ npm install ts-ioc-container reflect-metadata
|
|
|
25
54
|
yarn add ts-ioc-container reflect-metadata
|
|
26
55
|
```
|
|
27
56
|
|
|
28
|
-
|
|
29
|
-
### reflect-metadata
|
|
30
|
-
Just put it in the main file of your project. It should be the first line of the code.
|
|
57
|
+
Just put it in the entrypoint file of your project. It should be the first line of the code.
|
|
31
58
|
```typescript
|
|
32
59
|
import 'reflect-metadata';
|
|
33
60
|
```
|
|
34
61
|
|
|
35
|
-
|
|
62
|
+
And `tsconfig.json` should have next options:
|
|
36
63
|
```json
|
|
37
64
|
{
|
|
38
65
|
"compilerOptions": {
|
|
@@ -42,8 +69,10 @@ import 'reflect-metadata';
|
|
|
42
69
|
}
|
|
43
70
|
```
|
|
44
71
|
|
|
45
|
-
|
|
46
|
-
|
|
72
|
+
* * *
|
|
73
|
+
|
|
74
|
+
## Container
|
|
75
|
+
`IContainer` consists of 2 main parts:
|
|
47
76
|
|
|
48
77
|
- Providers - describes how to create instances of dependencies
|
|
49
78
|
- Injector - describes how to inject dependencies to constructor
|
|
@@ -165,8 +194,10 @@ describe('Disposing', function () {
|
|
|
165
194
|
|
|
166
195
|
```
|
|
167
196
|
|
|
168
|
-
|
|
169
|
-
|
|
197
|
+
* * *
|
|
198
|
+
|
|
199
|
+
## Injectors
|
|
200
|
+
`IInjector` is used to describe how dependencies should be injected to constructor.
|
|
170
201
|
|
|
171
202
|
- `ReflectionInjector` - injects dependencies using `@inject` decorator
|
|
172
203
|
- `ProxyInjector` - injects dependencies as dictionary `Record<string, unknown>`
|
|
@@ -299,8 +330,10 @@ describe('ProxyInjector', function () {
|
|
|
299
330
|
|
|
300
331
|
```
|
|
301
332
|
|
|
302
|
-
|
|
303
|
-
|
|
333
|
+
* * *
|
|
334
|
+
|
|
335
|
+
## Providers
|
|
336
|
+
`IProvider<T>` is used to describe how instances should be created. It has next basic methods:
|
|
304
337
|
- `resolve` - creates instance with passed arguments
|
|
305
338
|
- `clone` - we invoke it when we create a scope. It clones provider to new scope.
|
|
306
339
|
- `isValid` - checks if provider can be resolved from container or cloned to container with certain tags
|
|
@@ -457,9 +490,13 @@ describe('ArgsProvider', function () {
|
|
|
457
490
|
|
|
458
491
|
```
|
|
459
492
|
|
|
493
|
+
* * *
|
|
494
|
+
|
|
460
495
|
## Container modules
|
|
461
496
|
Sometimes you want to encapsulate registration logic in separate module. This is what `IContainerModule` is for.
|
|
462
497
|
|
|
498
|
+
### Basic usage
|
|
499
|
+
|
|
463
500
|
```typescript
|
|
464
501
|
import 'reflect-metadata';
|
|
465
502
|
import { IContainerModule, Registration, IContainer, key, Container, ReflectionInjector } from 'ts-ioc-container';
|
|
@@ -502,7 +539,7 @@ describe('Container Modules', function () {
|
|
|
502
539
|
|
|
503
540
|
```
|
|
504
541
|
|
|
505
|
-
|
|
542
|
+
### Registration module (Provider + DependencyKey)
|
|
506
543
|
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.
|
|
507
544
|
|
|
508
545
|
```typescript
|
|
@@ -523,6 +560,8 @@ describe('Registration module', function () {
|
|
|
523
560
|
|
|
524
561
|
```
|
|
525
562
|
|
|
563
|
+
* * *
|
|
564
|
+
|
|
526
565
|
## Hooks
|
|
527
566
|
Sometimes you need to invoke methods after construct or dispose of class. This is what hooks are for.
|
|
528
567
|
|
|
@@ -624,7 +663,9 @@ describe('Hooks', function () {
|
|
|
624
663
|
|
|
625
664
|
```
|
|
626
665
|
|
|
627
|
-
|
|
666
|
+
* * *
|
|
667
|
+
|
|
668
|
+
## Tests and Mocks
|
|
628
669
|
Sometimes you need to automatically mock all dependencies in container. This is what `AutoMockedContainer` is for.
|
|
629
670
|
|
|
630
671
|
```typescript
|
|
@@ -666,6 +707,7 @@ describe('Mocking', () => {
|
|
|
666
707
|
|
|
667
708
|
```
|
|
668
709
|
|
|
710
|
+
* * *
|
|
669
711
|
|
|
670
712
|
## Errors
|
|
671
713
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-ioc-container",
|
|
3
|
-
"version": "25.0.
|
|
3
|
+
"version": "25.0.4",
|
|
4
4
|
"description": "Typescript IoC container",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"ts-node": "^10.9.1",
|
|
57
57
|
"typescript": "4.4.3"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "02c953490e29c0a46b6609ae22ca6b1a688406e6"
|
|
60
60
|
}
|