Skip to content

Commit 50ef2ce

Browse files
author
Umed Khudoiberdiev
committed
updating docs and js example
1 parent 918b05d commit 50ef2ce

File tree

6 files changed

+137
-57
lines changed

6 files changed

+137
-57
lines changed

README.md

Lines changed: 116 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
3030
someClass.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);
7272
coffeeMaker.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

77159
1. Install module:
@@ -209,25 +291,7 @@ let coffeeMaker = Container.get(CoffeeMaker);
209291
coffeeMaker.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
233297
import {Container, Service, Inject} from "typedi";
@@ -281,7 +345,7 @@ let coffeeMaker = Container.get<CoffeeMaker>("coffee.maker");
281345
coffeeMaker.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.
285349
For 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

304395
You 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

419493
There 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.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export class BeanFactory {
1+
class BeanFactory {
22

33
create() {
44
console.log("bean created");
55
}
66

7-
}
7+
}
8+
9+
module.exports = { BeanFactory: BeanFactory };

sample/sample4-javascript/CoffeeMaker.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {BeanFactory} from "./BeanFactory";
2-
import {SugarFactory} from "./SugarFactory";
3-
import {WaterFactory} from "./WaterFactory";
1+
let BeanFactory = require("./BeanFactory").BeanFactory;
2+
let SugarFactory = require("./SugarFactory").SugarFactory;
3+
let WaterFactory = require("./WaterFactory").WaterFactory;
44

5-
export class CoffeeMaker {
5+
class CoffeeMaker {
66

77
constructor(container) {
88
this.beanFactory = container.get(BeanFactory);
@@ -19,4 +19,6 @@ export class CoffeeMaker {
1919
console.log("coffee is made. Authorization token: " + this.authorizationToken);
2020
}
2121

22-
}
22+
}
23+
24+
module.exports = { CoffeeMaker: CoffeeMaker };
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export class SugarFactory {
1+
class SugarFactory {
22

33
create() {
44
console.log("sugar created");
55
}
66

7-
}
7+
}
8+
9+
module.exports = { SugarFactory: SugarFactory };
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export class WaterFactory {
1+
class WaterFactory {
22

33
create() {
44
console.log("water created");
55
}
66

7-
}
7+
}
8+
9+
module.exports = { WaterFactory: WaterFactory };

sample/sample4-javascript/app.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import "reflect-metadata";
2-
import {Container} from "../../src/index";
3-
import {CoffeeMaker} from "./CoffeeMaker";
1+
var Container = require("../../build/compiled/src/index").Container;
2+
var CoffeeMaker = require("./CoffeeMaker").CoffeeMaker;
43

54
Container.of("my-container").set("authorizationToken", "!@#$%^&*");
65
let coffeeMaker = Container.of("my-container").get(CoffeeMaker);

0 commit comments

Comments
 (0)