mainline
Yet another javascript DI framework
Install
$ npm install --save mainline
Compatible with Node v6.2 and up.
Usage
import Mainline, {inject, injectable} from 'mainline';
const SOME_VALUE = 'path/to/something';
Mainline.registerVariable(SOME_VALUE, 'SOME_VALUE');
@injectable()
class DataStore {
constructor() {
...
}
}
@inject(['DataStore', 'SOME_VALUE'])
class DataService {
constructor(datastore, value) {
this.datastore = datastore,
this.value = value;
}
}
const data = new DataService(); // Mainline automatically provides parameters
Documentation
Mainline
#register
Registers the target and makes it globally available.
Mainline.register(someFunction, options);
Arguments
-
target
(Object|Class|Function|Primitive) target to make available throughout the app. -
options
(Object)-
alias
optional (String) Name by which to resolve it by. If none is provided the function or class name is used. -
type
optional (injectTypes) You can specify if it is a CLASS (Default) | SINGLETON | FUNC | VALUE.
-
Returns
-
target
(Object|Class|Function|Primitive) The target.
#registerFunc
Registers a target function and makes it globally available.
Mainline.registerFunc(target, alias);
Arguments
-
target
(Function) Function to make available throughout the app. -
alias
optional (String) Alias for the function. If not provided the function name is used.
Returns
-
target
(Object|Class|Function|Primitive) The target.
#registerVariable
Registers a target variable and makes it globally available.
Mainline.registerVariable(target, alias);
Arguments
-
target
(Object|Primitive) Instance to make available throughout the app. -
alias
required (String) Alias for the variable.
Returns
-
target
(Object|Primitive) The target.
#registerSingleton
Registers a target class/constructor and makes it globally available.
Mainline.registerSingleton(target, alias);
Arguments
-
target
(Class|constructor) class to make available throughout the app. After it is instantiated the first time the same instance is reused. -
alias
optional (String) Alias for the class. If not provided the class/constructor name is used.
Returns
-
target
(Object) The target.
#resolve
Method returns a resolver with which to generate the required objects or values.
Mainline.resolve(targetName1, targetName2, ...)
Arguments
-
targetName
(String) Names of objects to resolve.
Returns
-
Mainline
(Object) This is an object that can be used to retrieve or create dependencies by their name or alias.
#get
Instance method that will give access to those objects specified in resolve
method.
const resolver = Mainline.resolve('Item1', 'Item2');
const item1 = resolver.get('Item1').item;
Arguments
-
name
(String) Name or alias of dependency.
Returns
-
Resolver
(Object) An instance of a resolver which wraps the dependency.
Resolver
The resolver is a wrapper for a dependency which will allow you to create instances of the dependency (if it is a class) and override any function/constructor parameters.
#withParams
Used to provide parameters to bind to a function or pass to a constructor. Parameters must be provided in the order the target expects them.
const func = resolver.get('func1').withParams(param1, param2, ...).item;
Arguments
-
param
(Any) Any type needed by the target.
#item
Property used to get an instance of or reference to the desired target.
const obj1 = resolver.get('Class1').item;
const obj2 = resolver.get('Class2').withParams(true, 42).item;
Returns
- A reference to the required dependency.
@injectable
A decorator which can be used to register any class as an injectable dependency.
@injectable()
class Thing1 {
...
}
@injectable('Thing2', injectTypes.SINGLETON)
class Thingy{
...
}
Arguments
-
alias
optional (String) An alias to the dependency used toinject
it later. If one is not provided the class name is used by default. -
injectType
optional (Symbol) Used to specify what kind of dependency it is. CLASS (default) | SINGLETON. SINGLETON will guarantee the same instance will be returned throughout the application.
@inject
A decorator to specify that registered dependencies should be injected into the constructor. Parameters will be automatically passed whenever the class is instantiated.
@inject(['Thing1', 'Thing2'])
class Story {
constructor(thing1, thing2) {
...
}
}
const story1 = new Story(); // No need to provide parameters.
const story2 = new Story(undefined , 'OtherThing') // You can override injected parameters.
// Undefined params will be replaced with an injected instance.
injectTypes
A collection of Symbols used specify different kinds of dependencies. CLASS | SINGLETON | FUNC | VALUE
Examples
Coming Soon