container-ioc
Advanced tools
Comparing version
{ | ||
"name": "container-ioc", | ||
"version": "1.6.3", | ||
"version": "1.6.4", | ||
"description": "Dependency Injection and IoC container", | ||
@@ -5,0 +5,0 @@ "author": "Alexander Kozlov", |
@@ -40,3 +40,3 @@  | ||
class Service implements IService { | ||
constructor(@Inject('ISerivce') public service: IService) {} | ||
constructor(@Inject('IService') public service: IService) {} | ||
} | ||
@@ -52,3 +52,29 @@ | ||
``` | ||
### Best Practise: use InjectionToken instances for tokens instead of string/class literals: | ||
##### Without InjectionToken: | ||
```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); | ||
``` | ||
### Examples: | ||
@@ -80,31 +106,2 @@ * [using factory](https://github.com/thohoh/container-ioc/blob/master/examples/use-factory.ts) | ||
### Injection Token | ||
> InjectionToken helps to avoid hardcoded strings in your code. | ||
##### Without InjectionToken: | ||
```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); | ||
``` | ||
### Hierarchical containers. | ||
@@ -111,0 +108,0 @@ > if a provider wasn't found in a container it will look up in ascendant containers if there are any: |
23085
0.03%142
-2.07%