@@ -25,8 +25,8 @@ class SomeClass {
2525
2626}
2727
28- var Container = require (" typedi" );
29- let someClass = Container .get (SomeClass);
28+ var Container = require (" typedi" ). Container ;
29+ var someClass = Container .get (SomeClass);
3030someClass .someMethod ();
3131```
3232
@@ -67,11 +67,93 @@ class CoffeeMaker {
6767
6868}
6969
70- var container = require (" typedi" );
71- var coffeeMaker = container .get (CoffeeMaker);
70+ var Container = require (" typedi" ). Container ;
71+ var coffeeMaker = Container .get (CoffeeMaker);
7272coffeeMaker .make ();
7373```
7474
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+
75157## Usage with TypeScript
76158
771591 . Install module:
@@ -209,25 +291,7 @@ let coffeeMaker = Container.get(CoffeeMaker);
209291coffeeMaker .make ();
210292```
211293
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:
231295
232296``` typescript
233297import {Container , Service , Inject } from " typedi" ;
@@ -281,7 +345,7 @@ let coffeeMaker = Container.get<CoffeeMaker>("coffee.maker");
281345coffeeMaker .make ();
282346```
283347
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.
285349For example:
286350
287351``` typescript
@@ -299,6 +363,33 @@ class UserRepository {
299363}
300364```
301365
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+
302393### Services with token name
303394
304395You can use a services with a ` Token ` instead of name or target class.
@@ -397,23 +488,6 @@ class Car {
397488}
398489```
399490
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-
417491### Problem with circular references
418492
419493There is a known issue in language that it can't handle circular references. For example:
@@ -658,5 +732,4 @@ ormUseContainer(Container);
658732
659733## Samples
660734
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