Skip to content

Commit 6311b88

Browse files
#76 Added ES6+ examples, Updated docs
1 parent b41546e commit 6311b88

11 files changed

+151
-3
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
## **container-ioc**
5-
is [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) and [IoC container](http://martinfowler.com/articles/injection.html) for Typescript projects (ES6+ API is coming). It manages the dependencies between classes, so that applications stay easy to change and maintain as they grow.
5+
is [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) and [IoC container](http://martinfowler.com/articles/injection.html) for both **Typescript** and **ES6+** projects. It manages the dependencies between classes, so that applications stay easy to change and maintain as they grow.
66

77
[![npm version](https://badge.fury.io/js/container-ioc.svg)](https://badge.fury.io/js/container-ioc)
88
[![Build Status](https://travis-ci.org/thohoh/container-ioc.svg?branch=master)](https://travis-ci.org/thohoh/container-ioc)
@@ -11,12 +11,12 @@ is [Dependency Injection](https://en.wikipedia.org/wiki/Dependency_injection) an
1111

1212
### Features:
1313
* Well-known Angular4+ DI API.
14+
* Can be used in ES6+/Typescript projects.
1415
* No external dependencies.
1516
* Singleton and Non-Singleton configuration.
1617
* Hierarchical containers.
1718
* Pluggable metadata annotator.
18-
* Can be used both on front-end and back-end.
19-
* 100% test coverage.
19+
* 97% test coverage.
2020

2121
#### Installation:
2222
```
@@ -25,6 +25,7 @@ npm install --save container-ioc
2525

2626
### Quick start
2727

28+
##### Typescript:
2829
```typescript
2930
import { Container, Inject, Injectable } from 'container-ioc';
3031

@@ -48,6 +49,21 @@ let providers = [
4849
container.register(providers);
4950
let app = container.resolve(App);
5051
```
52+
53+
### Javascript ES6+:
54+
##### use alternative syntax for declaring injections shown below and don't use interfaces.
55+
```javascript
56+
57+
@Injectable(['IService'])
58+
class Service implements IService {
59+
constructor(service) {
60+
this.service = service;
61+
}
62+
}
63+
```
64+
65+
### NOTE: All the examples below are written in Typescript. Check out [examples/javascript](examples/javascript) and [examples/typescript](examples/typescript) for reference.
66+
5167
### Best Practise: use InjectionToken instances for tokens instead of string/class literals:
5268

5369
##### Without InjectionToken:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Container, Injectable, InjectionToken } from 'container-ioc';
2+
3+
const TService = new InjectionToken('IService');
4+
5+
@Injectable()
6+
class Service {}
7+
8+
const container = new Container();
9+
10+
container.register([
11+
{ token: TService, useClass: Service }
12+
]);
13+
14+
const service = container.resolve(TService);
File renamed without changes.
File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Container, Injectable } from 'container-ioc';
2+
3+
const container = new Container();
4+
5+
interface IService {
6+
[key: string]: any;
7+
}
8+
9+
@Injectable(['IService'])
10+
class App {
11+
constructor(private service: IService) {}
12+
}
13+
14+
container.register([
15+
App,
16+
{ token: 'IService', useValue: {}}
17+
]);
18+
19+
const app = container.resolve(App);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Container, Injectable, LifeTime } from 'container-ioc';
2+
3+
const container = new Container();
4+
5+
@Injectable()
6+
class A {}
7+
8+
container.register({ token: A, useClass: A, lifeTime: LifeTime.PerRequest });
9+
10+
const instance1 = container.resolve(A);
11+
const instance2 = container.resolve(A);
12+
13+
// instance1 !== instance2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Container, Injectable } from 'container-ioc';
2+
3+
// Not recommended
4+
5+
@Injectable() class App {}
6+
const container = new Container();
7+
8+
container.register(App);
9+
10+
const app = container.resolve(App);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Container, Injectable, Inject } from 'container-ioc';
2+
3+
const container = new Container();
4+
5+
interface IService {
6+
[key: string]: any;
7+
}
8+
9+
@Injectable()
10+
class App {
11+
constructor(@Inject('IUseFactory') private service: IService) {}
12+
}
13+
14+
@Injectable()
15+
class Service {}
16+
17+
container.register([
18+
App,
19+
{ token: 'IService', useClass: Service },
20+
{
21+
token: 'IUseFactory',
22+
useFactory: (service: IService) => {
23+
return service;
24+
},
25+
inject: ['IService']
26+
}
27+
]);
28+
29+
const app = container.resolve(App);

examples/typescript/use-factory.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Container, Injectable, Inject } from 'container-ioc';
2+
3+
const container = new Container();
4+
5+
interface IService {
6+
[key: string]: any;
7+
}
8+
9+
@Injectable()
10+
class App {
11+
constructor(@Inject('IUseFactory') private service: IService) {}
12+
}
13+
14+
@Injectable()
15+
class Service {}
16+
17+
container.register([
18+
App,
19+
{ token: 'IService', useClass: Service },
20+
{
21+
token: 'IUseFactory',
22+
useFactory: () => {
23+
return new Service();
24+
}
25+
}
26+
]);
27+
28+
const app = container.resolve(App);

0 commit comments

Comments
 (0)