inject.ts
TypeScript Dependency Injection Library
Node.js
Add it to your package.json
or install it with NPM:
$ npm install inject-ts
For using in scripts dependency
///
Quick Start
TypeScript Class Inner
Create a class and inject it into the Context
by adding the Class Decorator @Inject
. A Singleton of this class will
now be available in the Context
. This class can now be injected into another class through constructor injection.
;
TypeScript Class Outer -- Constructor Injection
Create a class that is added to the Context
. Use the Parameter Decorator @Provide(<T>)
passing in the parameter class
type of the parameters to the constructor. When this class is retrieved from Context
, the parameters will have been
injected.
;;
TypeScript Class Outer -- Property Injection
Create a class that is added to the Context
. Use the Property Decorator @Provide(<T>)
passing in the property class
type. When this class is retrieved from the Context
, the properties will have been injected.
;;
TypeScript Class Main
In the main class, get the outer class from the Context
by typing the return type and passing in the class type that
is being requested.
;; ;
Add an external object to the Context
External objects can be added to the Context
. First, create the object with all of it's requirements. Second, call
set(name:string, obj:any)
on the `Context' passing the name the object should be found by and the object.
; Context.getContext.set"<string name>", http;
Retrieve an external object from the Context
External objects can be retrieved from the context by passing in a name to the Decorator @Inject
.
;