Injector
Dependency injection for class injection and most of static decorator binding is resolved via injector.
export declare class Injector {
static createAndResolveChild(
parent: Injector,
Class: IProvider | Function,
providers: Array<IProvider | Function>
): Injector;
static createAndResolve(
Class: IProvider | Function,
providers: Array<IProvider | Function>
): Injector;
constructor(parent?: Injector, keys?: Array<any>);
createAndResolve(provider: IProvider, providers: Array<IProvider>): any;
destroy(): void;
has(key: any): boolean;
get(provider: any, Class?: IProvider): any;
set(key: any, value: Object): void;
getId(): string;
}
Injector.createAndResolveChild
Static method which creates child injector on current injector and creates instance of Injectable class.
All providers token and token dependencies are automatically verified by static implementation.
@param | @type | @description |
---|---|---|
parent | Injector | parent instance of injector |
Class | Iprovider or Function | class token to create instance of |
providers | Array<IProvider or Function> | list of dependencies of class token |
Usage:
@Injectable()
class MyInjectableClass{
@Inject("config")
private config: Object;
}
let parent = new Injector();
let injector = Injector.createAndResolveChild(
parent,
MyInjectableClass,
[
{provide: "config", useValue: {id: 1, message: "This is custom provider for injector"}}
]
);
let myInstance = injector.get(MyInjectableClass);
Injector.createAndResolve
Static method which creates injector and instance of Injectable class.
All providers token and token dependencies are automatically verified by static implementation.
@param | @type | @description |
---|---|---|
Class | Iprovider or Function | class token to create instance of |
providers | Array<IProvider or Function> | list of dependencies of class token |
Usage:
@Injectable()
class MyInjectableClass{
@Inject("config")
private config: Object;
}
let injector = Injector.createAndResolve(
MyInjectableClass,
[
{provide: "config", useValue: {id: 1, message: "This is custom provider for injector"}}
]
);
let myInstance = injector.get(MyInjectableClass);
constructor
Injector constructor
@param | @type | @description |
---|---|---|
parent | Injector | parent injector instance |
keys | Array<any> | list of keys which are mutable |
Usage:
// instance no parent injector and no mutable keys
let injector: Injector = new Injector();
injector.set('serviceA', 'valueA'); // serviceA
injector.set('serviceA', 'valueA1'); // throws exception you cannot reset value if is not set as mutable value
// instance no parent injector and mutable keys
let injector: Injector = new Injector(null, ['serviceA']);
injector.set('serviceA', 'valueA'); // injector sets serviceA to valueA
injector.set('serviceA', 'valueA1'); // injector sets serviceA to valueA1 no exception is thrown
// since serviceA is declared as mutable
// injector instance with parent injector and no mutable keys
let parent: Injector = new Injector();
parent.set('serviceA', 'valueA');
parent.set('serviceB', 'valueB');
let injector: Injector = new Injector(parent);
injector.set('serviceB', 'valueB1');
injector.get('serviceA'); // returns valueA
injector.get('serviceB'); // returns valueB1
createAndResolve
non static version of createAndResolve works only with verified providers
@param | @type | @description |
---|---|---|
provider | Iprovider | verified token provider |
providers | Array<IProvider> | list of verified provider dependencies |
Usage:
@Injectable()
class MyTokenB {
}
@Injectable()
class MyTokenA {
@Inject(MyTokenB)
private token: MyTokenB;
getToken() {
return this.token;
}
}
let injector: Injector = new Injector();
injector.createAndResolve(
Metadata.verifyProvider(MyTokenA),
Metadata.verifyProviders([
{
provide: MyTokenB,
useClass: MyTokenB
}
])
);
let tokenA1Init = injector.get(MyTokenA);
tokenA1Init.getToken(); // returns MyTokenB instance
destroy
destroy injector and all tokens and its child injector instances this method is mainly used by framework internally
let injector: Injector = new Injector();
injector.set('serviceA', 'valueA'); // serviceA
injector.set('serviceB', 'valueA'); // serviceB
injector.destory();
has
check if has defined provider in injector instance
@param | @type | @description |
---|---|---|
name | any | key |
let injector: Injector = new Injector();
injector.set('serviceA', 'valueA'); // serviceA
injector.set('serviceB', 'valueA'); // serviceB
injector.has('serviceA'); // true
injector.has('serviceB'); // true
set
this method is used internally by createAndResolve if you are not sure how injector works it's better to not use this method
@param | @type | @description |
---|---|---|
key | any | key |
value | Object | any value |
class A {
}
let injector: Injector = new Injector();
injector.set('serviceA', new A); // serviceA
get
gets value by token
@param | @type | @description |
---|---|---|
name | any (string, object, class, array) | key |
let injector: Injector = new Injector();
injector.set('serviceA', 'valueA'); // serviceA
injector.set('serviceB', 'valueA1'); // serviceB
injector.get('serviceA'); // valueA
injector.get('serviceB'); // valueA1
getId
get unique uuid generated on Injector init process, this is used only for debugging purposes
let injector: Injector = new Injector();
injector.getId(); // ab132zbf-bb76-4477-917b-df5d37c5088d