@@ -25,8 +25,8 @@ class SomeClass {
25
25
26
26
}
27
27
28
- var Container = require (" typedi" );
29
- let someClass = Container .get (SomeClass);
28
+ var Container = require (" typedi" ). Container ;
29
+ var someClass = Container .get (SomeClass);
30
30
someClass .someMethod ();
31
31
```
32
32
@@ -67,11 +67,93 @@ class CoffeeMaker {
67
67
68
68
}
69
69
70
- var container = require (" typedi" );
71
- var coffeeMaker = container .get (CoffeeMaker);
70
+ var Container = require (" typedi" ). Container ;
71
+ var coffeeMaker = Container .get (CoffeeMaker);
72
72
coffeeMaker .make ();
73
73
```
74
74
75
+ With TypeDI you can use a named services. Example:
76
+
77
+ ``` typescript
78
+ var Container = require (" typedi" ).Container ;
79
+
80
+ class BeanFactory implements Factory {
81
+ create() {
82
+ }
83
+ }
84
+
85
+ class SugarFactory implements Factory {
86
+ create() {
87
+ }
88
+ }
89
+
90
+ class WaterFactory implements Factory {
91
+ create() {
92
+ }
93
+ }
94
+
95
+ class CoffeeMaker {
96
+
97
+ beanFactory: Factory ;
98
+ sugarFactory: Factory ;
99
+ waterFactory: Factory ;
100
+
101
+ constructor (container ) {
102
+ this .beanFactory = container .get (" bean.factory" );
103
+ this .sugarFactory = container .get (" sugar.factory" );
104
+ this .waterFactory = container .get (" water.factory" );
105
+ }
106
+
107
+ make() {
108
+ this .beanFactory .create ();
109
+ this .sugarFactory .create ();
110
+ this .waterFactory .create ();
111
+ }
112
+
113
+ }
114
+
115
+ Container .set (" bean.factory" , new BeanFactory (Container ));
116
+ Container .set (" sugar.factory" , new SugarFactory (Container ));
117
+ Container .set (" water.factory" , new WaterFactory (Container ));
118
+ Container .set (" coffee.factory" , new CoffeeMaker (Container ));
119
+
120
+ var coffeeMaker = Container .get (" coffee.maker" );
121
+ coffeeMaker .make ();
122
+ ```
123
+
124
+ This feature especially useful if you want to store (and inject later on) some settings or configuration options.
125
+ For example:
126
+
127
+ ``` typescript
128
+ var Container = require (" typedi" ).Container ;
129
+
130
+ // somewhere in your global app parameters
131
+ Container .set (" authorization-token" , " RVT9rVjSVN" );
132
+
133
+ class UserRepository {
134
+
135
+ constructor (container ) {
136
+ this .authorizationToken = container .get (" authorization-token" );
137
+ }
138
+
139
+ }
140
+ ```
141
+
142
+ When you write tests you can easily provide your own "fake" dependencies to classes you are testing using ` set ` method:
143
+ ` provide ` methods of the container:
144
+
145
+ ``` typescript
146
+ Container .set (CoffeeMaker , new FakeCoffeeMaker ());
147
+
148
+ // or for named services
149
+
150
+ Container .set ([
151
+ { id: " bean.factory" , value: new FakeBeanFactory () },
152
+ { id: " sugar.factory" , value: new FakeSugarFactory () },
153
+ { id: " water.factory" , value: new FakeWaterFactory () }
154
+ ]);
155
+ ```
156
+
75
157
## Usage with TypeScript
76
158
77
159
1 . Install module:
@@ -209,25 +291,7 @@ let coffeeMaker = Container.get(CoffeeMaker);
209
291
coffeeMaker .make ();
210
292
```
211
293
212
- ## Advanced usage
213
-
214
- * [ Named services] ( #named-services )
215
- * [ Services with token name] ( #services-with-token-name )
216
- * [ Using factory function to create service] ( #using-factory-function-to-create-service )
217
- * [ Using factory class to create service] ( #using-factory-class-to-create-service )
218
- * [ Providing values to the container] ( #providing-values-to-the-container )
219
- * [ Problem with circular references] ( #problem-with-circular-references )
220
- * [ Inherited injections] ( #inherited-injections )
221
- * [ Custom decorators] ( #custom-decorators )
222
- * [ Using service groups] ( #using-service-groups )
223
- * [ Using multiple containers and scoped containers] ( #using-multiple-containers-and-scoped-containers )
224
- * [ Remove registered services or reset container state] ( #remove-registered-services-or-reset-container-state )
225
-
226
-
227
- ### Named services
228
-
229
- You can use a named services.
230
- This feature is especially useful when you want to create a service for the interface.
294
+ With TypeDI you can use a named services. Example:
231
295
232
296
``` typescript
233
297
import {Container , Service , Inject } from " typedi" ;
@@ -281,7 +345,7 @@ let coffeeMaker = Container.get<CoffeeMaker>("coffee.maker");
281
345
coffeeMaker .make ();
282
346
```
283
347
284
- This feature is also useful if you want to store (and inject later on) some settings or configuration options.
348
+ This feature especially useful if you want to store (and inject later on) some settings or configuration options.
285
349
For example:
286
350
287
351
``` typescript
@@ -299,6 +363,33 @@ class UserRepository {
299
363
}
300
364
```
301
365
366
+ When you write tests you can easily provide your own "fake" dependencies to classes you are testing using ` set ` method:
367
+ ` provide ` methods of the container:
368
+
369
+ ``` typescript
370
+ Container .set (CoffeeMaker , new FakeCoffeeMaker ());
371
+
372
+ // or for named services
373
+
374
+ Container .set ([
375
+ { id: " bean.factory" , value: new FakeBeanFactory () },
376
+ { id: " sugar.factory" , value: new FakeSugarFactory () },
377
+ { id: " water.factory" , value: new FakeWaterFactory () }
378
+ ]);
379
+ ```
380
+
381
+ ## TypeScript Advanced Usage Examples
382
+
383
+ * [ Services with token name] ( #services-with-token-name )
384
+ * [ Using factory function to create service] ( #using-factory-function-to-create-service )
385
+ * [ Using factory class to create service] ( #using-factory-class-to-create-service )
386
+ * [ Problem with circular references] ( #problem-with-circular-references )
387
+ * [ Inherited injections] ( #inherited-injections )
388
+ * [ Custom decorators] ( #custom-decorators )
389
+ * [ Using service groups] ( #using-service-groups )
390
+ * [ Using multiple containers and scoped containers] ( #using-multiple-containers-and-scoped-containers )
391
+ * [ Remove registered services or reset container state] ( #remove-registered-services-or-reset-container-state )
392
+
302
393
### Services with token name
303
394
304
395
You can use a services with a ` Token ` instead of name or target class.
@@ -397,23 +488,6 @@ class Car {
397
488
}
398
489
```
399
490
400
- ### Providing values to the container
401
-
402
- If you are writing unit tests for you class, you may want to provide fakes to your classes. You can use ` set ` or
403
- ` provide ` methods of the container:
404
-
405
- ``` typescript
406
- Container .set (CoffeeMaker , new FakeCoffeeMaker ());
407
-
408
- // or
409
-
410
- Container .set ([
411
- { id: " bean.factory" , value: new FakeBeanFactory () },
412
- { id: " sugar.factory" , value: new FakeSugarFactory () },
413
- { id: " water.factory" , value: new FakeWaterFactory () }
414
- ]);
415
- ```
416
-
417
491
### Problem with circular references
418
492
419
493
There is a known issue in language that it can't handle circular references. For example:
@@ -658,5 +732,4 @@ ormUseContainer(Container);
658
732
659
733
## Samples
660
734
661
- Take a look on samples in [ ./sample] ( https://github.com/pleerock/typedi/tree/master/sample ) for more examples of
662
- usages.
735
+ Take a look on samples in [ ./sample] ( https://github.com/pleerock/typedi/tree/master/sample ) for examples of usage.
0 commit comments