Skip to content

Commit 07ccc09

Browse files
NoNameProvidedgitbook-bot
authored andcommitted
GitBook: [develop] 3 pages modified
1 parent 45b0974 commit 07ccc09

File tree

3 files changed

+85
-65
lines changed

3 files changed

+85
-65
lines changed

README.md

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1+
---
2+
description: Simple yet powerful dependency injection tool for JavaScript and TypeScript.
3+
---
4+
15
# TypeDI
26

3-
![Build Status](https://github.com/typestack/typedi/workflows/CI/badge.svg)
4-
[![codecov](https://codecov.io/gh/typestack/typedi/branch/master/graph/badge.svg)](https://codecov.io/gh/typestack/typedi)
5-
[![npm version](https://badge.fury.io/js/typedi.svg)](https://badge.fury.io/js/typedi)
6-
[![Dependency Status](https://david-dm.org/typestack/typedi.svg)](https://david-dm.org/typestack/typedi)
7+
![Build Status](https://github.com/typestack/typedi/workflows/CI/badge.svg) [![codecov](https://codecov.io/gh/typestack/typedi/branch/master/graph/badge.svg)](https://codecov.io/gh/typestack/typedi) [![npm version](https://badge.fury.io/js/typedi.svg)](https://badge.fury.io/js/typedi) [![Dependency Status](https://david-dm.org/typestack/typedi.svg)](https://david-dm.org/typestack/typedi)
78

8-
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.
1010

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)
1313

1414
## Usage with JavaScript
1515

1616
Install the module:
1717

1818
`npm install typedi --save`
1919

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:
2221

2322
```javascript
2423
class SomeClass {
@@ -30,8 +29,7 @@ var someClass = Container.get(SomeClass);
3029
someClass.someMethod();
3130
```
3231

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`.
3533

3634
In your class's constructor you always receive as a last argument a container which you can use to get other dependencies.
3735

@@ -111,8 +109,7 @@ var coffeeMaker = Container.get('coffee.maker');
111109
coffeeMaker.make();
112110
```
113111

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:
116113

117114
```javascript
118115
var Container = require('typedi').Container;
@@ -187,23 +184,22 @@ console.log(postController);
187184

188185
`npm install reflect-metadata --save`
189186

190-
and import it somewhere in the global place of your app before any service declaration or import (for example in `app.ts`):
187+
and import it somewhere in the global place of your app before any service declaration or import \(for example in `app.ts`\):
191188

192189
`import "reflect-metadata";`
193190

194191
3. You may need to install node typings:
195192

196193
`npm install @types/node --save-dev`
197194

198-
4) Enabled following settings in `tsconfig.json`:
195+
4\) Enabled following settings in `tsconfig.json`:
199196

200-
```json
197+
```javascript
201198
"emitDecoratorMetadata": true,
202199
"experimentalDecorators": true,
203200
```
204201

205-
Now you can use TypeDI.
206-
The most simple usage example is:
202+
Now you can use TypeDI. The most simple usage example is:
207203

208204
```typescript
209205
import 'reflect-metadata';
@@ -218,8 +214,7 @@ let someClass = Container.get(SomeClass);
218214
someClass.someMethod();
219215
```
220216

221-
Then you can call `Container.get(SomeClass)` from anywhere in your application
222-
and you'll always have the same instance of `SomeClass`.
217+
Then you can call `Container.get(SomeClass)` from anywhere in your application and you'll always have the same instance of `SomeClass`.
223218

224219
You can use **property injection** and inject services into your class using `@Inject` decorator:
225220

@@ -350,8 +345,7 @@ let coffeeMaker = Container.get<CoffeeMaker>('coffee.maker');
350345
coffeeMaker.make();
351346
```
352347

353-
This feature especially useful if you want to store (and inject later on) some settings or configuration options.
354-
For example:
348+
This feature especially useful if you want to store \(and inject later on\) some settings or configuration options. For example:
355349

356350
```typescript
357351
import { Container, Service, Inject } from 'typedi';
@@ -366,8 +360,7 @@ class UserRepository {
366360
}
367361
```
368362

369-
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:
371364

372365
```typescript
373366
Container.set(CoffeeMaker, new FakeCoffeeMaker());
@@ -383,20 +376,19 @@ Container.set([
383376

384377
## TypeScript Advanced Usage Examples
385378

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)
395388

396389
### Services with token name
397390

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.
400392

401393
```typescript
402394
import { Container, Service, Inject, Token } from 'typedi';
@@ -436,8 +428,7 @@ factory.create();
436428

437429
You can create your services with the container using factory functions.
438430

439-
This way, service instance will be created by calling your factory function instead of
440-
instantiating a class directly.
431+
This way, service instance will be created by calling your factory function instead of instantiating a class directly.
441432

442433
```typescript
443434
import { Container, Service } from 'typedi';
@@ -462,8 +453,7 @@ console.log(car.engineType); // > "V8"
462453

463454
You can also create your services using factory classes.
464455

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.
467457

468458
```typescript
469459
import { Container, Service } from 'typedi';
@@ -503,8 +493,7 @@ export class Engine {
503493
}
504494
```
505495

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:
508497

509498
```typescript
510499
// Car.ts
@@ -526,8 +515,7 @@ And that's all. Same for constructor injections.
526515

527516
### Inherited injections
528517

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:
531519

532520
```typescript
533521
// Car.ts
@@ -546,8 +534,7 @@ export class Bus extends Car {
546534

547535
### Custom decorators
548536

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:
551538

552539
```typescript
553540
// Logger.ts
@@ -585,8 +572,7 @@ export class UserRepository {
585572

586573
### Using service groups
587574

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:
590576

591577
```typescript
592578
// Factory.ts
@@ -630,13 +616,9 @@ factories.forEach(factory => factory.create());
630616

631617
### Using multiple containers and scoped containers
632618

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.
635620

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:
640622

641623
```typescript
642624
// QuestionController.ts
@@ -667,12 +649,9 @@ controller2.save('');
667649
Container.reset(request2);
668650
```
669651

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.
672653

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:
676655

677656
```typescript
678657
@Service({ global: true })
@@ -719,18 +698,15 @@ console.log(postController);
719698

720699
### Remove registered services or reset container state
721700

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.
725702

726703
## Troubleshooting
727704

728705
### Use TypeDI with routing-controllers and/or TypeORM
729706

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).
732708

733-
```Typescript
709+
```typescript
734710
import {useContainer as routingUseContainer} from "routing-controllers";
735711
import {useContainer as ormUseContainer} from "typeorm";
736712
import {Container} from "typedi";
@@ -742,3 +718,4 @@ ormUseContainer(Container);
742718
## Samples
743719

744720
Take a look on samples in [./sample](https://github.com/pleerock/typedi/tree/master/sample) for examples of usage.
721+

SUMMARY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Table of contents
2+
3+
* [TypeDI](README.md)
4+
* [Changelog](changelog.md)
5+

changelog.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Changelog
2+
3+
## 0.8.0
4+
5+
* added new type of dependency injection - function DI
6+
* now null can be stored in the container for values
7+
8+
## 0.7.2
9+
10+
* fixed bug with inherited services
11+
12+
## 0.7.1
13+
14+
* fixed the way how global services work
15+
16+
## 0.7.0
17+
18+
* added javascript support
19+
* removed deprecated `@Require` decorator
20+
* added support for transient services
21+
* now service constructors cannot accept non-service arguments
22+
* added `@InjectMany` decorator to support injection of "many" values
23+
* fixed the way how global services work
24+
25+
## 0.6.1
26+
27+
* added `Container.has` method
28+
29+
## 0.6.0
30+
31+
* added multiple containers support
32+
* added grouped \(tagged\) containers support
33+
* removed `provide` method, use `set` method instead
34+
* deprecated `Require` decorator. Use es6 imports instead or named services
35+
* inherited classes don't need to be decorated with `@Service` decorator
36+
* other small api changes
37+
* now `Handler`'s `value` accepts a container which requests the value
38+

0 commit comments

Comments
 (0)