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
[](https://gitter.im/pleerock/typedi?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://gitter.im/typestack/typedi?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7
7
8
-
Simple yet powerful dependency injection tool for TypeScript.
8
+
TypeDI is a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) tool for TypeScript.
9
+
Using TypeDI you can build well-structured and easily tested applications.
9
10
10
11
## Installation
11
12
@@ -37,7 +38,7 @@ and you have enabled following settings in `tsconfig.json`:
37
38
38
39
## Usage
39
40
40
-
If you simply want to use a container:
41
+
The most simple usage example is:
41
42
42
43
```typescript
43
44
import {Container} from"typedi";
@@ -53,7 +54,26 @@ let someClass = Container.get(SomeClass);
53
54
someClass.someMethod();
54
55
```
55
56
56
-
If you want to inject other classes into your service you can do:
57
+
Then you can call `Container.get(SomeClass)` from anywhere in your application
58
+
and you'll always have the same instance of `SomeClass`.
59
+
60
+
If you want to use more advanced functionality you need to mark your class with `@Service` decorator:
61
+
62
+
```typescript
63
+
import {Service} from"typedi";
64
+
65
+
@Service()
66
+
classSomeClass {
67
+
68
+
someMethod() {
69
+
}
70
+
71
+
}
72
+
```
73
+
74
+
Its recommended to always use `@Service` decorator on your service classes.
75
+
76
+
You can services into your class using `@Inject` decorator:
57
77
58
78
```typescript
59
79
import {Container, Inject, Service} from"typedi";
@@ -100,7 +120,7 @@ let coffeeMaker = Container.get(CoffeeMaker);
100
120
coffeeMaker.make();
101
121
```
102
122
103
-
If you want to use constructor injection:
123
+
You can also use a constructor injection:
104
124
105
125
```typescript
106
126
import {Container, Service} from"typedi";
@@ -142,38 +162,25 @@ let coffeeMaker = Container.get(CoffeeMaker);
142
162
coffeeMaker.make();
143
163
```
144
164
145
-
> note: Your classes may not to have `@Service` decorator to use it with Container, however its recommended to add
146
-
`@Service` decorator to all classes you are using with container, because without `@Service` decorator applied
147
-
constructor injection may not work properly in your classes.
0 commit comments