Appolo Inject
es6 Dependency Injection framework for nodejs build with typescript.
appolo-inject
is part for the appolo framework.
Installation
npm install appolo-inject --save
Typescript
appolo-inject
requires TypeScript compiler version > 2.1 and the following settings in tsconfig.json
:
Usage
### Creating injection container
var inject = ; var injector = inject; injector; injector;
Add Definitions
the definition object key is used for object class id.
var inject = ; var injector = inject; injector; injector;
or with injector define
let injector = inject; injector; injector; //get fooController instance from the injector let fooController = injector;
or with decorators
@ //or with custom id@ let injector = inject; injector; injector; //get fooController instance from the injector let fooController = injector;//orlet fooController2 = injector; let someController = injector;
Get Object
get object from the injector by id or class name if the object is not singleton you will get new instance every time.
@ let injector = inject; injector; injector; let fooController = injectorget<FooController>'fooController'; let fooController2 = injectorget<FooController>'fooController'; console // false
Singleton
the class will be created only once and injector
will return the same instance every time.
@@{} let injector = inject; injector;injector; let fooController = injectorget<FooController>FooController; let fooController2 = injectorget<FooController>'fooController'; console // true
Inject Constructor Arguments
you can inject objects to constructor arguments you can inject object instance by id or by value.
it is not recommended to inject objects to constructor because you can easily get circular reference.
@@ { return 'foo' } @ { thisfooManager = fooManager; thisname = name; } { return thisfooManagername +thisname } var injector = inject; injector; injector; var buzzController = injectorgetObject<BuzzController>BuzzController"buzz"; console // foobuzz
Inject with runtime arguments
@ { thisname = name; } { return thisname } let injector = inject; injector;injector; let buzzController = injectorget<BuzzController>'buzzController''buzz'; console // buzz
Init Method
The init method
will be called after all instances were created and all the properties injected.
@@ {return 'foo'; } @ @ fooManager:FooManager @ { thisname = thisfooManagername } {return thisname} let injector = inject;injector; injector; var fooController = injector; fooControllername // foo
Inject Instance
inject
will try to inject object id to the same property name.
@@ { return 'foo' } @@ { return 'bar' } @ @ fooManager:FooManager; @ barManager:BarManager; { return thisfooManagername + thisbarManagername } var injector = inject; injector; injector; var buzzController = injectorget<BuzzController>BuzzController; console // foobar
Inject Instance By Name
you can set the name of the property the object will be injected to.
@@ {return 'foo'} @@ { return 'bar'} @ @ foo:FooManager; @ bar:BarManager; { return thisfooname + thisbarname} var injector = inject; injector; injector; var buzzController = injector; console // foobar ```
Inject Property Value
you can inject any value to object property.
@define() class FooManager{ @injectValue('foo') name:string get name () {return this.name;} } @define() class BuzzController{ @inject(FooManager) foo:FooManager; get name () { return this.foo.name}} let injector = inject.createContainer(); injector.registerMulti([BuzzController,FooManager]); injector.initialize(); let buzzController = injector.get('buzzController'); console.log(buzzController.name()) // foo
Inject Method Param
you can inject instance to method param.
@ {return "foo"} @ public return thisfooname let injector = inject; injector; injector; let buzzController = injector; console // foo
Inject Factory
factory object must have implement get
method that will be called in order to inject the object instance.
the get
method can return promise;
@@ {return 'bar'; } @@@ implements IFactory<BarManager> @ barManager:BarManager; async :Promise<BarManager> return thisbarManager; @ @ foo:BarManager {return thisfooname} let injector = inject; injector; injector; var buzzController = injector; console // bar
Inject Factory Method
factory method is a function that will return the injected object.
this is useful the create many instances of the same class.
@ { thisname = name; } {return thisname; } @ @ Person { return thisname; } let injector = inject;injector; injector; var buzzController = injector; console // foo
Alias
you can add alias names to classes and get all the classes by single alias. all the alias must be singletons
interface IHandler name:string @@@ implements IHandler {return 'foo'} @@@ implements IHandler {return 'bar'} @ @ allHandlers:IHandler { return thisallHandlers; } let injector = inject;injector; injector; var buzzController = injector; buzzControllername // foobar
Alias Factory
you can add alias factory names to classes and get all the classes new instance by factory method.
interface IHandler name:string @@ implements IHandler { } get :string return this_name @@ implements IHandler public name:string { } get :string return this_name @ @ allHandlers:IHandler { return thisallHandlers; } let injector = inject;injector; injector; var buzzController = injector; buzzControllername // 01
Inject Property Array
you can inject array
of properties by reference
or by value
.
@@ { return 'foo' } @@ {return 'bar'} @ @ objects:any { thisobjects } let injector = inject; injector; injector; var buzzController = injector; buzzControllername // foobar
Inject Property Dictionary
you can inject dictionary
of properties by reference
or by value
.
@@ { return 'foo' } @@ {return 'bar'} @ @ objects:any {return thisobjectsfooname + thisobjectsbarname + thisobjectsbaz;} let injector = inject; injector; injector; var buzzController = injector; buzzControllername // foobarbaz
Inject Property From Object Property
you can inject property from other object property.
@@ public name = 'foo'; @ @ otherObjectProperty {return return thisotherObjectProperty;} let injector = inject; injector; injector; let buzzController = injector; buzzControllername // foo
Injector Aware
you can get reference to the injector container by adding injectorAware
the injector will be injected to $injector
property.
@@ @ { this$injector }
Tests
grunt test
License
The appolo inject
library is released under the MIT license. So feel free to modify and distribute it as you wish.