container-ioc
Advanced tools
Comparing version
@@ -6,3 +6,3 @@ <a name="1.6.8"></a> | ||
* **Added support for ES6+** Added new syntax for declaring injectioins of a class, now it's possible via Injectable decorator. see [examples/javascript](examples/javascript). | ||
* **Added support for ES6+** - Added new syntax for declaring injectioins of a class, now it's possible via Injectable decorator. see [examples/javascript](examples/javascript). | ||
@@ -14,3 +14,3 @@ <a name="1.6.0"></a> | ||
* **Persistence control** added LifeTime managment configuration, see [example](examples/typescript/persistence-control.ts) [5ba097a9cb](https://github.com/thohoh/container-ioc/commit/5ba097a9cb41277e0e9013d4ef5e694f3595de36) | ||
* **Life Time control** - added LifeTime managment configuration, see [example](examples/typescript/life-time-control.ts) [5ba097a9cb](https://github.com/thohoh/container-ioc/commit/5ba097a9cb41277e0e9013d4ef5e694f3595de36) | ||
@@ -32,3 +32,3 @@ <a name="1.5.0"></a> | ||
* **@Injectable() decorator:** now to make a class available for injection you have to mark it with @Injectable() decorator: [86fede13](https://github.com/thohoh/container-ioc/commit/86fede13be7147079c36bc77e204ac21deb360bc) | ||
* **@Injectable() decorator:** - now to make a class available for injection you have to mark it with @Injectable() decorator: [86fede13](https://github.com/thohoh/container-ioc/commit/86fede13be7147079c36bc77e204ac21deb360bc) | ||
@@ -54,4 +54,4 @@ ### Breaking Changes | ||
* **Value and Factory:** support using values and factories for registrations. commit | ||
* **Value and Factory:** - container now can resolve values or anything returned from a factory. commit | ||
[50ebb63](https://github.com/thohoh/container-ioc/commit/50ebb63451878b262626446828f7b7ac5ce6afe5) | ||
* **Injection Token** added InjectionToken class to facilitate working with abstractions. [3c380c878](https://github.com/thohoh/container-ioc/commit/3c380c878abef883b293007f97299d5053eafe5b) | ||
* **Injection Token** - added InjectionToken class to facilitate working with abstractions. [3c380c878](https://github.com/thohoh/container-ioc/commit/3c380c878abef883b293007f97299d5053eafe5b) |
{ | ||
"name": "container-ioc", | ||
"version": "1.6.9", | ||
"version": "1.6.10", | ||
"description": "Dependency Injection and IoC container", | ||
@@ -5,0 +5,0 @@ "author": "Alexander Kozlov", |
146
README.md
@@ -14,9 +14,12 @@  | ||
* Well-known Angular4+ DI API. | ||
* Can be used in ES6+/Typescript projects. | ||
* ES6+/Typescript. | ||
* No external dependencies. | ||
* Singleton and Non-Singleton configuration. | ||
* Life Time control. | ||
* Hierarchical containers. | ||
* Pluggable metadata annotator. | ||
* Resolves values using Classes, Factories and Values. | ||
* Descriptive error messages. | ||
* 97% test coverage. | ||
### Quick start | ||
#### Installation: | ||
@@ -27,5 +30,3 @@ ``` | ||
### Quick start | ||
##### Typescript: | ||
#### Typescript: | ||
```typescript | ||
@@ -52,7 +53,8 @@ import { Container, Inject, Injectable } from 'container-ioc'; | ||
container.register(providers); | ||
let app = container.resolve(App); | ||
``` | ||
### Javascript ES6+: | ||
##### use alternative syntax for declaring injections shown below and don't use interfaces. | ||
#### Javascript ES6+: | ||
> Use alternative syntax for declaring injections shown below and don't use interfaces. See [examples/javascript](examples/javascript) for more. | ||
```javascript | ||
@@ -68,33 +70,11 @@ | ||
### NOTE: All the examples below are written in Typescript. Check out [examples/javascript](examples/javascript) and [examples/typescript](examples/typescript) for examples. | ||
### Examples: | ||
* [examples/javascript](examples/javascript) | ||
* [examples/typescript](examples/typescript) | ||
### Best Practise: use InjectionToken instances for tokens instead of string/class literals: | ||
## Code examples below are written in Typescript. | ||
##### Without InjectionToken: | ||
### Life Time control. | ||
> By default, containers resolve singletons. You can change that by setting provider's attribute **LifeTime** to **LifeTime.PerRequest**. | ||
```typescript | ||
interface IService {} | ||
@Injectable() | ||
class ConcreteService {} | ||
container.register({ token: 'IService', useClass: ConcreteService }); | ||
container.resolve('IService'); | ||
``` | ||
##### With InjectionToken | ||
```typescript | ||
interface IService {} | ||
const TService = new InjectionToken<IService>('IService'); // T stands for Token, you can pick another prefix | ||
@Injectable() | ||
class ConcreteService {} | ||
container.register({ token: TService, useClass: ConcreteService }); | ||
container.resolve(TService); | ||
``` | ||
### Persistence control. | ||
> By default, resolved instances are singletons. You can change that by setting provider's attribute **LifeTime** to **LifeTime.PerRequest**. | ||
```typescript | ||
import { Container, Injectable, LifeTime } from 'container-ioc'; | ||
@@ -107,13 +87,12 @@ | ||
container.register({ token: A, useClass: A, lifeTime: LifeTime.PerRequest }); | ||
container.register([ | ||
{ token: A, useClass: A, lifeTime: LifeTime.PerRequest } | ||
]); | ||
const instance1 = container.resolve(A); | ||
const instance2 = container.resolve(A); | ||
// instance1 !== instance2 | ||
``` | ||
### Hierarchical containers. | ||
> if a provider wasn't found in a container it will look up in ascendant containers if there are any: | ||
> If a provider wasn't found in a container it will look up in ascendant containers if there are any: | ||
```typescript | ||
@@ -134,23 +113,80 @@ import { Container } from 'container-ioc'; | ||
### Pluggable metadata annotator. | ||
> By default metadata is assigned to static properties. | ||
> If you want to use Reflect API for annotation, you can implement **IMetadataAnnotator** interface with your implementation using Reflect API. Then plug it into **AnnotatorProvider** | ||
### Using Factories | ||
```typescript | ||
import { AnnotatorProvider, IMetadataAnnotator, Container } from 'container-ioc'; | ||
/* Without injections */ | ||
container.register([ | ||
{ | ||
token: 'TokenForFactory', | ||
useFactory: () => { | ||
return 'any-value'; | ||
} | ||
} | ||
]); | ||
class ReflectMetadataAnnotator implements IMetadataAnnotator { | ||
// your implementation | ||
} | ||
/* With injections */ | ||
container.register([ | ||
{ token: 'EnvProvider', useClass: EnvProvider }, | ||
{ | ||
token: 'TokenForFactory', | ||
useFactory: (envProvider) => { | ||
// do something | ||
return 'something'; | ||
}, | ||
inject: ['EnvProvider'] | ||
} | ||
]); | ||
``` | ||
AnnotatorProvider.set(new ReflectMetadataAnnotator()); | ||
### Using Values | ||
```typescript | ||
container.register([ | ||
{ token: 'IConfig', useValue: {}} | ||
]); | ||
``` | ||
let container = new Container(); | ||
### Shortcut for Classes | ||
```typescript | ||
container.register([ | ||
App | ||
]); | ||
``` | ||
Is the same as: | ||
```typescript | ||
container.register([ | ||
{ token: App, useClass: App } | ||
]); | ||
``` | ||
... | ||
### Best Practise, use InjectionToken | ||
> Use **InjectionToken** instances for tokens instead of string/class literals, | ||
it saves from using hardcoded string and **helps in keeping abstractions intact**. | ||
Before: | ||
```typescript | ||
interface IService {} | ||
@Injectable() | ||
class ConcreteService {} | ||
container.register({ token: 'IService', useClass: ConcreteService }); | ||
container.resolve('IService'); | ||
``` | ||
After: | ||
### Contribution: | ||
Feel free to submit a bug or a feature request. | ||
Or pick an issue from [here](https://github.com/thohoh/container-ioc/issues) and leave a comment with your proposal. | ||
```typescript | ||
interface IService {} | ||
see [CONTRIBUTION.md](CONTRIBUTION.md) | ||
const TService = new InjectionToken<IService>('IService'); // T stands for Token, you can pick another prefix | ||
@Injectable() | ||
class ConcreteService {} | ||
container.register({ token: TService, useClass: ConcreteService }); | ||
container.resolve(TService); | ||
``` | ||
## Contribution: | ||
Become a contributor to this project. Feel free to submit an [issue](https://github.com/thohoh/container-ioc/issues) or a pull request. | ||
see [CONTRIBUTION.md](CONTRIBUTION.md) for more information. |
24210
1.6%34
3.03%186
24%