You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeDI is a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) tool for JavaScript and TypeScript.
9
-
Using TypeDI you can build well-structured and easily tested applications.
9
+
TypeDI is a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) tool for JavaScript and TypeScript. Using TypeDI you can build well-structured and easily tested applications.
10
10
11
-
-[Usage with JavaScript](#usage-with-javascript)
12
-
-[Usage with TypeScript](#usage-with-typescript)
11
+
*[Usage with JavaScript](./#usage-with-javascript)
12
+
*[Usage with TypeScript](./#usage-with-typescript)
13
13
14
14
## Usage with JavaScript
15
15
16
16
Install the module:
17
17
18
18
`npm install typedi --save`
19
19
20
-
Now you can use TypeDI.
21
-
The most simple usage example is:
20
+
Now you can use TypeDI. The most simple usage example is:
22
21
23
22
```javascript
24
23
classSomeClass {
@@ -30,8 +29,7 @@ var someClass = Container.get(SomeClass);
30
29
someClass.someMethod();
31
30
```
32
31
33
-
Then you can call `Container.get(SomeClass)` from anywhere in your application
34
-
and you'll always have the same instance of `SomeClass`.
32
+
Then you can call `Container.get(SomeClass)` from anywhere in your application and you'll always have the same instance of `SomeClass`.
35
33
36
34
In your class's constructor you always receive as a last argument a container which you can use to get other dependencies.
37
35
@@ -111,8 +109,7 @@ var coffeeMaker = Container.get('coffee.maker');
111
109
coffeeMaker.make();
112
110
```
113
111
114
-
This feature especially useful if you want to store (and inject later on) some settings or configuration options.
115
-
For example:
112
+
This feature especially useful if you want to store \(and inject later on\) some settings or configuration options. For example:
When you write tests you can easily provide your own "fake" dependencies to classes you are testing using `set` method:
370
-
`provide` methods of the container:
363
+
When you write tests you can easily provide your own "fake" dependencies to classes you are testing using `set` method: `provide` methods of the container:
371
364
372
365
```typescript
373
366
Container.set(CoffeeMaker, newFakeCoffeeMaker());
@@ -383,20 +376,19 @@ Container.set([
383
376
384
377
## TypeScript Advanced Usage Examples
385
378
386
-
-[Services with token name](#services-with-token-name)
387
-
-[Using factory function to create service](#using-factory-function-to-create-service)
388
-
-[Using factory class to create service](#using-factory-class-to-create-service)
389
-
-[Problem with circular references](#problem-with-circular-references)
390
-
-[Inherited injections](#inherited-injections)
391
-
-[Custom decorators](#custom-decorators)
392
-
-[Using service groups](#using-service-groups)
393
-
-[Using multiple containers and scoped containers](#using-multiple-containers-and-scoped-containers)
394
-
-[Remove registered services or reset container state](#remove-registered-services-or-reset-container-state)
379
+
*[Services with token name](./#services-with-token-name)
380
+
*[Using factory function to create service](./#using-factory-function-to-create-service)
381
+
*[Using factory class to create service](./#using-factory-class-to-create-service)
382
+
*[Problem with circular references](./#problem-with-circular-references)
383
+
*[Inherited injections](./#inherited-injections)
384
+
*[Custom decorators](./#custom-decorators)
385
+
*[Using service groups](./#using-service-groups)
386
+
*[Using multiple containers and scoped containers](./#using-multiple-containers-and-scoped-containers)
387
+
*[Remove registered services or reset container state](./#remove-registered-services-or-reset-container-state)
395
388
396
389
### Services with token name
397
390
398
-
You can use a services with a `Token` instead of name or target class.
399
-
In this case you can use type safe interface-based services.
391
+
You can use a services with a `Token` instead of name or target class. In this case you can use type safe interface-based services.
You can also create your services using factory classes.
464
455
465
-
This way, service instance will be created by calling given factory service's method factory instead of
466
-
instantiating a class directly.
456
+
This way, service instance will be created by calling given factory service's method factory instead of instantiating a class directly.
467
457
468
458
```typescript
469
459
import { Container, Service } from'typedi';
@@ -503,8 +493,7 @@ export class Engine {
503
493
}
504
494
```
505
495
506
-
This code will not work, because Engine has a reference to Car, and Car has a reference to Engine.
507
-
One of them will be undefined and it cause errors. To fix them you need to specify a type in a function this way:
496
+
This code will not work, because Engine has a reference to Car, and Car has a reference to Engine. One of them will be undefined and it cause errors. To fix them you need to specify a type in a function this way:
508
497
509
498
```typescript
510
499
// Car.ts
@@ -526,8 +515,7 @@ And that's all. Same for constructor injections.
526
515
527
516
### Inherited injections
528
517
529
-
Inherited injections are supported as well. In order to use them you must mark inherited class as a @Service.
530
-
For example:
518
+
Inherited injections are supported as well. In order to use them you must mark inherited class as a @Service. For example:
531
519
532
520
```typescript
533
521
// Car.ts
@@ -546,8 +534,7 @@ export class Bus extends Car {
546
534
547
535
### Custom decorators
548
536
549
-
You can create your own decorators which will inject your given values for your service dependencies.
550
-
For example:
537
+
You can create your own decorators which will inject your given values for your service dependencies. For example:
551
538
552
539
```typescript
553
540
// Logger.ts
@@ -585,8 +572,7 @@ export class UserRepository {
585
572
586
573
### Using service groups
587
574
588
-
You can group multiple services into single group tagged with service id or token.
589
-
For example:
575
+
You can group multiple services into single group tagged with service id or token. For example:
### Using multiple containers and scoped containers
632
618
633
-
By default all services are stored in the global service container,
634
-
and this global service container holds all unique instances of each service you have.
619
+
By default all services are stored in the global service container, and this global service container holds all unique instances of each service you have.
635
620
636
-
If you want your services to behave and store data inside differently,
637
-
based on some user context (http request for example) -
638
-
you can use different containers for different contexts.
639
-
For example:
621
+
If you want your services to behave and store data inside differently, based on some user context \(http request for example\) - you can use different containers for different contexts. For example:
640
622
641
623
```typescript
642
624
// QuestionController.ts
@@ -667,12 +649,9 @@ controller2.save('');
667
649
Container.reset(request2);
668
650
```
669
651
670
-
In this example `controller1` and `controller2` are completely different instances,
671
-
and `QuestionRepository` used in those controllers are different instances as well.
652
+
In this example `controller1` and `controller2` are completely different instances, and `QuestionRepository` used in those controllers are different instances as well.
672
653
673
-
`Container.reset` removes container with the given context identifier.
674
-
If you want your services to be completely global and not be container-specific,
675
-
you can mark them as global:
654
+
`Container.reset` removes container with the given context identifier. If you want your services to be completely global and not be container-specific, you can mark them as global:
### Remove registered services or reset container state
721
700
722
-
If you need to remove registered service from container simply use `Container.remove(...)` method.
723
-
Also you can completely reset the container by calling `Container.reset()` method.
724
-
This will effectively remove all registered services from the container.
701
+
If you need to remove registered service from container simply use `Container.remove(...)` method. Also you can completely reset the container by calling `Container.reset()` method. This will effectively remove all registered services from the container.
725
702
726
703
## Troubleshooting
727
704
728
705
### Use TypeDI with routing-controllers and/or TypeORM
729
706
730
-
In order to use typedi with routing-controllers and/or typeorm, it's **necessary** to tell these libs to use the typedi container.
731
-
Otherwise you may face [this kind of issue](https://github.com/pleerock/typedi/issues/4).
707
+
In order to use typedi with routing-controllers and/or typeorm, it's **necessary** to tell these libs to use the typedi container. Otherwise you may face [this kind of issue](https://github.com/pleerock/typedi/issues/4).
0 commit comments