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)
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,13 +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.
165
+
## Advanced usage
166
+
167
+
*[Named services](#named-services)
168
+
*[Services with token name](#services-with-token-name)
169
+
*[Using factory function to create service](#using-factory-function-to-create-service)
170
+
*[Using factory class to create service](#using-factory-class-to-create-service)
171
+
*[Providing values to the container](#providing-values-to-the-container)
172
+
*[Problem with circular references](#problem-with-circular-references)
173
+
*[Inherited injections](#inherited-injections)
174
+
*[Custom decorators](#custom-decorators)
175
+
*[Using service groups](#using-service-groups)
176
+
*[Using multiple containers and scoped containers](#using-multiple-containers-and-scoped-containers)
177
+
*[Remove registered services or reset container state](#remove-registered-services-or-reset-container-state)
178
+
148
179
149
180
### Named services
150
181
151
-
You can use a named services. In this case you can use interface-based services.
182
+
You can use a named services.
183
+
This feature is especially useful when you want to create a service for the interface.
152
184
153
185
```typescript
154
186
import {Container, Service, Inject} from"typedi";
@@ -202,6 +234,24 @@ let coffeeMaker = Container.get<CoffeeMaker>("coffee.maker");
202
234
coffeeMaker.make();
203
235
```
204
236
237
+
This feature is also useful if you want to store (and inject later on) some settings or configuration options.
0 commit comments