mp-inject
A type-based dependency injection library.
Installation
$ npm install --save mp-inject
Requirments
// tsconfig.json
Getting started
; ;console.logservice instanceof Service; //trueconsole.logservice.http instanceof Http; //trueconsole.logservice.logger instanceof Logger; //trueconsole.logservice.http.logger instanceof Logger; //true
API
Injector.clearSingletons(type?: Function|string|number|symbol) : void
Clear singleton of specified type, if type is omitted, clear all singletons's type.
- type: The singleton type to clear
Injector.register(type: Function|string|number|symbol, value:Function | object, options?:object) : void
Register a service for injection.
- type: The service type to be register.
- value: The associated value can be a factory function.
- options: The registration options.
-
- alias(Function|string|number|symbol, optional): The another name for current Service.
; Injector.register"String", "default value";Injector.register0,Math.random; Injector.registerService,new MyService;
Injector.registerClass(type: Function|string|number|symbol, value:new(...args: any) => any, options?:object) : void
Register a class service for injection.
- type: The service type to be register.
- value: The associated value must be a class.
- options: The registration options.
-
- alias(Function|string|number|symbol, optional): The another name for current Service.
; Injector.registerClassService, MyService;// equal to // Injector.register(Service, (...args:any)=>new MyService(...args));
Injector.get(type: Function|string|number|symbol, ...args: any[]): T
Get the value corresponding to a specific type, the type must be registered in advance. If the type was't injected in advance, an error will be throw.
- type: The type registered.
- args: The parameters required by the factory function.
; Injector.registerDemo, new Demo;;
Injector.getOrDefault(type: Function|string|number|symbol, defaultValue: T ...args: any[]): T
Get the value corresponding to a specific type, If the type was't injected in advance, an default value will be returned.
- type: The type registered.
- defaultValue: The value returned by default.
- args: The parameters required by the factory function.
; ;
Singleton
The
Injectable
should be the topmost decorator
; Injector.registerNumber,Math.random; ;;console.logservice1.num === service2.num //falseconsole.logservice1 === service2 //false;;console.logservice3.num === service4.num //trueconsole.logservice3 === service4 //true
Clear Singleton
Singleton.clearSingletonService;// orInjector.clearSingletonsSingletonService;// or clear all singletonsInjector.clearSingletons;
Alias
; Injector.registerClassService1, Service1, ; console.logInjector.get'service1' instanceof Service1 //trueconsole.logInjector.getService1 instanceof Service1 //trueconsole.logInjector.get'demo1' instanceof Demo1 //trueconsole.logInjector.getDemo1 instanceof Demo1 //trueconsole.logInjector.get'demo1'.service1 instanceof Service1 //true
Poperties Injection
; Injector.registerNumber, 0;
Optional
By default, if the injected properties or parameters are absent, an error will be thrown. If the properties or parameters are optional, you can use the Optional
decorator.
;
Also see
auto-validate validate object by decorator in typescript.
auto-mapping: map and convert objects automatically in typescript;