easy-injectionJS
A package that provides dependency injection using common design patterns as stereotypes. It easily injects depedencies in runtime and supports inheritance:
There's no additional dependencies or containers needed to use the package. You can simply use it as follows:
npm i -S easy-injectionjs
Or
yarn add easy-injectionjs
Dependencies without inheritance:
; // Creates a singleton instance of Movie class in runtime@ { // Lets say that they just release the movie :D thisname = 'Black Panther'; thisratings = 90; // I really like that movie } public : string return thisname; public : number return thisratings; public { thisname = v; } public { thisratings = v; } // Creates a multiple instances as needed in runtime@ @ private movie: Movie; private name: string; { thisname = name; } public : string return thismovie; public { thismovie; } let T_Challa = 'Chadwick Boseman';console;// Output is Black PantherT_Challa; // Hopefullyconsole; // Output is Black Panther IIlet Erik_Killmonger = 'Michael B Jordan';console // // Output is Black Panther II
If the movie had the annotaion of EasyPrototype instead, Erik_Killmonger.getMovieName() will return Black Panther istead of Black Panther 2.
Interfaces and named dependencies example:
; interface Show : string; : number; ; ; // Creates a singleton instance of Movie class in runtime@ implements Show { // Lets say that they just release the movie :D thisname = 'Black Panther'; thisratings = 90; // I really like that movie } public : string return thisname; public : number return thisratings; public { thisname = v; } public { thisratings = v; } // Creates a multiple instances as needed in runtime@ @ private movie: Show; private name: string; { thisname = name; } public : string return thismovie; public { thismovie; } let T_Challa = 'Chadwick Boseman';console;// Output is Black PantherT_Challa; // Hopefullyconsole; // Output is Black Panther IIlet Erik_Killmonger = 'Michael B Jordan';console // // Output is Black Panther II
The package support inheritance even with abstract classes using the @EasyFactory decorator:
Example:
; @abstract public abstract : string; public abstract : number; public abstract ; public abstract ; // Creates a singleton instance of Movie class in runtime@ { super; // Lets say that they just release the movie :D thisname = 'Black Panther'; thisratings = 90; // I really like that movie } public : string return thisname; public : number return thisratings; public { thisname = v; } public { thisratings = v; } // Creates a multiple instances as needed in runtime@ @ private movie: Show; private name: string; { thisname = name; } public : string return thismovie; public { thismovie; } let T_Challa = 'Chadwick Boseman';console;// Output is Black PantherT_Challa; // Hopefullyconsole; // Output is Black Panther IIlet Erik_Killmonger = 'Michael B Jordan';console // // Output is Black Panther II
Both Iterfaces and abstract classes inheritance support naming to differentiate between the children dependencies and the names have to go into the @Easy decorator and have to match the names in the @EasyPrototype and @EasySingleton decorators. You can even name the @EasyFactory decorator if u have many abstract classes extending each others.
You can even not name anything and let the package figure out which dependencies as needed:
; @abstract abstract ; abstract ; // @EasyObservable()@ // @Easy() { super thisname = 'Sal'; } public { return thisname; } public { thisname = v; } @ @ somebody: Person; { super } public { return thissomebody; } public { thissomebody; } @ @ somebody: Person; name: string; { thissomebody; } : string return thissomebody; let n = ;console // Prints Saln;console // Prints awesomelet d = console // Prints awesomedconsole // Prints Gelbadconsole // Prints Kaa
The "is" Keyword:
The "is" function is used to retrieve dependencies anywhere in the program. It can also be usint with constructor injection:
Example:
; @ // @Easy() { thisname = 'Vladi'; } public { return thisname; } public { thisname = v; } @ private _name: string; { someone this_name = someone; console; } // call returns an instance of Awesome console // returns Sal since it is a singleton :D
The "Easily" Method:
The "Easily" is ideally used to define static constants or simple objects like:
; const config = author: 'Sal';
It requires a name to be injected. PS it can work for npm packages also, if imported like this: import * as smth from 'package'
@ @ config: Object; { super } public { return thissomebody; } public { thissomebody; }
A bit complicated example:
; @abstract abstract ; abstract ; @ // @Easy() { super thisname = 'Sal'; } public { return thisname; } public { thisname = v; } @ @ somebody: Person; @ config: Object; { super } public { return thissomebody; } public { thissomebody; } @abstract public abstract : string; public abstract : number; public abstract ; public abstract ; // Creates a singleton instance of Movie class in runtime@ { super; // Lets say that they just release the movie :D thisname = 'Black Panther'; thisratings = 90; // I really like that movie } public : string return thisname; public : number return thisratings; public { thisname = v; } public { thisratings = v; } @ @ somebody: Person; name: string; @ movie: Movie; { thissomebody; } : string return thissomebody; let n = ;consoleconsole // Prints Saln;console // Prints awesomelet d = consoleconsole // Prints awesomedconsole // Prints Gelbadconsole
Even method injection is very easy using both "Easily", the decorators and "is". That makes dynamic injection very easy.
; ;
And somewhere in the program in another file:
; { dataname = 'I am Batman'; // update it ;}
And maybe another class requires "Data":
;// why not :D@ @ private data:any;
If modifyData was called before Mule was used, the data name inside the property data of mule will change in runtime.