Skip to content

Commit 7a1d932

Browse files
docs: rework documentation
1 parent b276707 commit 7a1d932

15 files changed

+645
-235
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Main features includes:
1919
> Note: This installation guide is for usage with TypeScript, if you wish to use
2020
> TypeDI without Typescript please read the documentation about how get started.
2121
22-
First install the required packages via:
22+
To start using TypeDI install the required packages via NPM:
2323

2424
```bash
2525
npm install typedi reflect-metadata

docs/README.md

Lines changed: 1 addition & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -3,218 +3,7 @@
33
TypeDI is a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) tool for JavaScript and TypeScript.
44
Using TypeDI you can build well-structured and easily tested applications.
55

6-
- [Usage with JavaScript](#usage-with-javascript)
7-
- [Usage with TypeScript](#usage-with-typescript)
8-
9-
## Usage with JavaScript
10-
11-
Install the module:
12-
13-
`npm install typedi --save`
14-
15-
Now you can use TypeDI.
16-
The most simple usage example is:
17-
18-
```javascript
19-
class SomeClass {
20-
someMethod() {}
21-
}
22-
23-
var Container = require('typedi').Container;
24-
var someClass = Container.get(SomeClass);
25-
someClass.someMethod();
26-
```
27-
28-
Then you can call `Container.get(SomeClass)` from anywhere in your application
29-
and you'll always have the same instance of `SomeClass`.
30-
31-
In your class's constructor you always receive as a last argument a container which you can use to get other dependencies.
32-
33-
```javascript
34-
class BeanFactory {
35-
create() {}
36-
}
37-
38-
class SugarFactory {
39-
create() {}
40-
}
41-
42-
class WaterFactory {
43-
create() {}
44-
}
45-
46-
class CoffeeMaker {
47-
constructor(container) {
48-
this.beanFactory = container.get(BeanFactory);
49-
this.sugarFactory = container.get(SugarFactory);
50-
this.waterFactory = container.get(WaterFactory);
51-
}
52-
53-
make() {
54-
this.beanFactory.create();
55-
this.sugarFactory.create();
56-
this.waterFactory.create();
57-
}
58-
}
59-
60-
var Container = require('typedi').Container;
61-
var coffeeMaker = Container.get(CoffeeMaker);
62-
coffeeMaker.make();
63-
```
64-
65-
With TypeDI you can use a named services. Example:
66-
67-
```javascript
68-
var Container = require('typedi').Container;
69-
70-
class BeanFactory implements Factory {
71-
create() {}
72-
}
73-
74-
class SugarFactory implements Factory {
75-
create() {}
76-
}
77-
78-
class WaterFactory implements Factory {
79-
create() {}
80-
}
81-
82-
class CoffeeMaker {
83-
beanFactory: Factory;
84-
sugarFactory: Factory;
85-
waterFactory: Factory;
86-
87-
constructor(container) {
88-
this.beanFactory = container.get('bean.factory');
89-
this.sugarFactory = container.get('sugar.factory');
90-
this.waterFactory = container.get('water.factory');
91-
}
92-
93-
make() {
94-
this.beanFactory.create();
95-
this.sugarFactory.create();
96-
this.waterFactory.create();
97-
}
98-
}
99-
100-
Container.set('bean.factory', new BeanFactory(Container));
101-
Container.set('sugar.factory', new SugarFactory(Container));
102-
Container.set('water.factory', new WaterFactory(Container));
103-
Container.set('coffee.maker', new CoffeeMaker(Container));
104-
105-
var coffeeMaker = Container.get('coffee.maker');
106-
coffeeMaker.make();
107-
```
108-
109-
This feature especially useful if you want to store (and inject later on) some settings or configuration options.
110-
For example:
111-
112-
```javascript
113-
var Container = require('typedi').Container;
114-
115-
// somewhere in your global app parameters
116-
Container.set('authorization-token', 'RVT9rVjSVN');
117-
118-
class UserRepository {
119-
constructor(container) {
120-
this.authorizationToken = container.get('authorization-token');
121-
}
122-
}
123-
```
124-
125-
When you write tests you can easily provide your own "fake" dependencies to classes you are testing using `set` method:
126-
127-
```javascript
128-
Container.set(CoffeeMaker, new FakeCoffeeMaker());
129-
130-
// or for named services
131-
132-
Container.set([
133-
{ id: 'bean.factory', value: new FakeBeanFactory() },
134-
{ id: 'sugar.factory', value: new FakeSugarFactory() },
135-
{ id: 'water.factory', value: new FakeWaterFactory() },
136-
]);
137-
```
138-
139-
TypeDI also supports a function dependency injection. Here is how it looks like:
140-
141-
```javascript
142-
var Service = require('typedi').Service;
143-
var Container = require('typedi').Container;
144-
145-
var PostRepository = Service(() => ({
146-
getName() {
147-
return 'hello from post repository';
148-
},
149-
}));
150-
151-
var PostManager = Service(() => ({
152-
getId() {
153-
return 'some post id';
154-
},
155-
}));
156-
157-
class PostQueryBuilder {
158-
build() {
159-
return 'SUPER * QUERY';
160-
}
161-
}
162-
163-
var PostController = Service([PostManager, PostRepository, PostQueryBuilder], (manager, repository, queryBuilder) => {
164-
return {
165-
id: manager.getId(),
166-
name: repository.getName(),
167-
query: queryBuilder.build(),
168-
};
169-
});
170-
171-
var postController = Container.get(PostController);
172-
console.log(postController);
173-
```
174-
175-
## Usage with TypeScript
176-
177-
1. Install module:
178-
179-
`npm install typedi --save`
180-
181-
2. Install [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) package:
182-
183-
`npm install reflect-metadata --save`
184-
185-
and import it somewhere in the global place of your app before any service declaration or import (for example in `app.ts`):
186-
187-
`import "reflect-metadata";`
188-
189-
3. You may need to install node typings:
190-
191-
`npm install @types/node --save-dev`
192-
193-
4) Enabled following settings in `tsconfig.json`:
194-
195-
```json
196-
"emitDecoratorMetadata": true,
197-
"experimentalDecorators": true,
198-
```
199-
200-
Now you can use TypeDI.
201-
The most simple usage example is:
202-
203-
```typescript
204-
import 'reflect-metadata';
205-
import { Service, Container } from 'typedi';
206-
207-
@Service()
208-
class SomeClass {
209-
someMethod() {}
210-
}
211-
212-
let someClass = Container.get(SomeClass);
213-
someClass.someMethod();
214-
```
215-
216-
Then you can call `Container.get(SomeClass)` from anywhere in your application
217-
and you'll always have the same instance of `SomeClass`.
6+
## Typescript Usage
2187

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

@@ -717,23 +506,3 @@ console.log(postController);
717506
If you need to remove registered service from container simply use `Container.remove(...)` method.
718507
Also you can completely reset the container by calling `Container.reset()` method.
719508
This will effectively remove all registered services from the container.
720-
721-
## Troubleshooting
722-
723-
### Use TypeDI with routing-controllers and/or TypeORM
724-
725-
In order to use typedi with routing-controllers and/or typeorm, it's **necessary** to tell these libs to use the typedi container.
726-
Otherwise you may face [this kind of issue](https://github.com/pleerock/typedi/issues/4).
727-
728-
```Typescript
729-
import {useContainer as routingUseContainer} from "routing-controllers";
730-
import {useContainer as ormUseContainer} from "typeorm";
731-
import {Container} from "typedi";
732-
733-
routingUseContainer(Container);
734-
ormUseContainer(Container);
735-
```
736-
737-
## Samples
738-
739-
Take a look on samples in [./sample](https://github.com/pleerock/typedi/tree/master/sample) for examples of usage.

docs/SUMMARY.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
# Table of contents
22

3-
- [TypeDI](README.md)
4-
- [Changelog](changelog.md)
3+
- [Old documentation](README.md)
4+
5+
- [Getting Started](typescript/01-getting-started.md)
6+
- [Usage Guide](typescript/02-basic-usage-guide.md)
7+
- [Container API](typescript/03-container-api.md)
8+
- [@Service decorator](typescript/04-service-decorator.md)
9+
- [@Inject decorator](typescript/05-inject-decorator.md)
10+
- [Service Tokens](typescript/06-service-tokens.md)
11+
- [Usage with TypeORM](typescript/07-usage-with-typeorm.md)
12+
- Advanced Usage
13+
- [Creating custom decorators](typescript/08-custom-decorators.md)
14+
- [Using scoped container](typescript/09-using-scoped-containers.md)
15+
- [Transient services](typescript/10-using-transient-services.md)
16+
17+
## Usage without TypeScript
18+
19+
- [Getting Started](javascript/01-getting-started.md)
20+
- Usage
21+
- [Old documentation](javascript/02-basic-usage.md)

docs/javascript/01-getting-started.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Getting started without TypeScript
2+
3+
It's possible to use TypeDI without TypesScript, however some of the functionality is limited or not available.
4+
These differences are listed below in the [Limitations][limitations-sections] section.
5+
6+
## Installation
7+
8+
To start using TypeDI with JavaScript install it via NPM:
9+
10+
```bash
11+
npm install typedi
12+
```
13+
14+
## Basic usage
15+
16+
The most basic usage is to request an instance of a class definition. TypeDI will check if an instance of the class has
17+
been created before and return the cached version or it will create a new instance, cache and return it.
18+
19+
```js
20+
import { Container } from 'typedi';
21+
22+
class ExampleClass {
23+
print() {
24+
console.log('I am alive!');
25+
}
26+
}
27+
28+
/** Request an instance of ExampleClass from TypeDI. */
29+
const classInstance = Container.get(ExampleClass);
30+
31+
/** We received an instance of ExampleClass and ready to work with it. */
32+
classInstance.print();
33+
```
34+
35+
For more advanced usage examples and patterns please read the [next page][basic-usage-page].
36+
37+
## Limitations
38+
39+
_To be written..._
40+
41+
[limitations-sections]: #limitations
42+
[basic-usage-page]: ./02-basic-usage.md

0 commit comments

Comments
 (0)