The di4js module is dependency injection implementation in JavaScript. Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them. di4js is free software distributed under the terms of the MIT License (MIT) and can be used with web browsers or with node.js.
var Car = function (engine, year) {
this.engine = engine;
this.year = year;
};
Car.prototype.start = function () {
this.engine.start();
};
var DieselEngine = function () {
this.hp = 0;
};
DieselEngine.prototype.start = function () {
console.log("Diesel engine with " + this.hp + " hp has been started...");
};
di
.autowired(false)
.register('dieselEngine')
.as(DieselEngine)
.withProperties()
.prop('hp').val(42);
.register('car')
.as(Car)
.withConstructor()
.param().ref('dieselEngine')
.param().val(1976);
var car = di.resolve('car');
car.start(); // Diesel engine with 42 hp has been started...
- Supported platforms
- Installation
- API
- License
- Authors
di4js runs on all the following environments:
Web browsers
Server-side platforms
To install di4js module for Node.js, this command should be used:
npm install di4js
Also di4js module's loading statement should be added to main module:
'use strict';
var di = require('di4js');
// Your code goes here...
Theare are few options to install di4js to the web browser. Module can be downloaded or can be installed using NuGet package manager.
<!DOCTYPE HTML>
<html>
<body>
<!-- Your code goes here...-->
<script src="./scripts/di4js.min.js"></script>
</body>
</html>
di4js is compatible with asynchronous module definition (AMD) and it can be loaded as ordinal module.
define(['di4js'], function (di) {
});
In Visual Studio di4js module can be installed using NuGet extension. To install di4js, run the following command in the package manager console.
Install-Package di4js
If autowired is enabled for dependency resolver, all type's or instance's dependencies are resolved automatically. By default autowired is disabled for dependency resolver. Parameter value is optional and has to be a boolean.
di
.autowired()
.register('engine')
.as(DieselEngine)
.withProperties()
.prop('hp').val(42);
.register('car')
.as(Car);
di.resolve('car'); // { engine: { hp: 42 } }
Allows to check if autowired is enabled.
di
.autowired();
di.isAutowired; // true
Defines registration's name. Parameter name is required and has to be a string.
di.register('dieselEngine');
Multiple registration's can be defined for a single name. A list of instances will be returned while resolving such registration by name.
di
.register('engine')
.instance(new DieselEngine())
.register('engine')
.instance(new PetrolEngine());
var engines = di.resolve('engine'); // []
Maps a name with the given type. Parameter type is required and has to be a function.
di.register('dieselEngine').as(DieselEngine);
Maps registration's name with the given instance. Parameter instance is required and can be any type.
var engine = new DieselEngine();
di.register('dieselEngine').instance(engine);
Various types can be used:
di.register('number').instance(0);
di.register('flag').instance(false);
di.register('str').instance('Hello world!');
di.register('date').instance(new Date());
di.register('func').instance(function () { });
di.register('obj').instance({});
Marks type as singleton. If type is marked as singleton, the same instance always will be returned.
di
.register('dieselEngine')
.as(DieselEngine)
.asSingleton();
Allows to define constructor's parameters.
Defines constructor's parameter. Parameter name is optional and has to be a string or integer. It can represent parameter's name or index.
Parameter can be defined without name. Index will be assigned automatically.
di
.register('car')
.as(Car)
.withConstructor()
.param().val(new DieselEngine())
.param().val(1976);
di.resolve('car'); // { engine: { hp: 0 }, year: 1976 }
Parameter can be defined by name or index.
di
.register('car')
.as(Car)
.withConstructor()
.param('engine').val(new DieselEngine())
.param(1).val(1976);
di.resolve('car'); // { engine: { hp: 0 }, year: 1976 }
Allows to define type's properties.
Defines property. Parameter name is required and has to be a string.
di
.register('dieselEngine')
.as(DieselEngine)
.withProperties()
.prop('hp').val(140);
Defines function which has to be invoked while resoling type. Parameter name is required and has to be a string.
di
.register('car')
.as(Car)
.withProperties()
.func('setEngine')
.param().ref('dieselEngine');
Allows to define property's or constructor paramter's value. Parameter instance is required and can be any type.
Constructor parameter's value is defined.
di
.register('dieselEngine')
.as(DieselEngine)
.withConstructor()
.param().val(140);
Property's value is defined.
di
.register('dieselEngine')
.as(DieselEngine)
.withProperties()
.prop('hp').val(140);
Function paramter's value is defined.
di
.register('car')
.as(Car)
.withProperties()
.func('setEngine')
.param().val(new DieselEngine());
Maps property or constructor's parameter with registered type in dependency resolver.
Constructor's parameter can be resolved while instantiating a type.
di
.register('car')
.withConstructor()
.param().ref('dieselEngine');
Property can be resolved while instantiating a type.
di
.register('car')
.withProperties()
.prop('engine').ref('dieselEngine');
Function's paramter can be resolved while resolving a type.
di
.register('car')
.as(Car)
.withProperties()
.func('setEngine')
.param().ref('dieselEngine');
Replaces default factory which is used while instantiating a new instance. Parameter factory is required and it accepts function or object.
A function can be used:
di
.register('dieselEngine')
.as(DieselEngine)
.setFactory(function (options) {
return new options.type();
});
An object which has a method create can be used:
var CustomFactory = {
create: function (options) {
return new options.type();
}
};
di
.register('dieselEngine')
.as(DieselEngine)
.setFactory(CustomFactory);
Resolves type or instance by name. Parameter name is required and has to be a string.
di.register('dieselEngine').as(DieselEngine);
var engine = di.resolve('dieselEngine');
Allows to create child container. It usefull when you want to have more when one container or container per specific scope, or to override parent's container do not making inpact on it.
var child = di.create();
child
.register('dieselEngine')
.as(DieselEngine);
di.contains('dieselEngine'); // false
child.contains('dieselEngine'); // true
Allows to to inject dependencies to the function or to the instantiated object.
Dependency are injected automatically by parameter's name to the function.
di
.register('dieselEngine')
.as(DieselEngine);
di.inject(function (dieselEngine) {
engine.start(); // Diesel engine with 0 hp has been started...
});
An array notation can be used for injecting dependencies to the function.
di
.register('dieselEngine')
.as(DieselEngine);
di.inject(['dieselEngine', function (dieselEngine) {
engine.start(); // Diesel engine with 0 hp has been started...
}]);
Properties of the object are set automatically by properties' names.
di
.register('engine')
.as(DieselEngine);
var car = new Car();
di.inject(car); // { engine: { hp: 0 }, year: null }
Registration's name, which has to be applied while injecting dependencies to the function, can be passed to inject method.
di
.register('dieselEngine')
.as(DieselEngine)
.register('car')
.as(Car)
.withConstructor()
.param().ref('dieselEngine')
.param().val(2000);
di.inject(function (engine, year) {
engine.start(); // Diesel engine with 0 hp has been started...
}, 'car');
Registration's name, which has to be applied while injecting dependencies to the object, can be passed to inject method.
di
.register('dieselEngine')
.as(DieselEngine)
.register('car')
.as(Car)
.withProperties()
.prop('engine').ref('dieselEngine');
var car = new Car();
di.inject(car, 'car');
Allows to check if type or instance is registered in dependency resolver with given name. Parameter name is required and has to be a string.
di.resolve('dieselEngine'); // false
Replaces default factory of dependecy resolver. Factory is used to create instance from type's definition. Parameter factory is required and it accepts function or object.
A function can be used.
di
.setDefaultFactory(function (options) {
return new options.type();
})
.register('dieselEngine')
.as(DieselEngine);
An object which has method created can be used.
var customFactory = {
create: function options() {
return new options.type();
}
};
di
.setDefaultFactory(customFactory)
.register('dieselEngine')
.as(DieselEngine);
It is used to transform property's or constructor's parameter's name while resolving type's dependencies. Parameter transformer is required and it accepts function or object.
A function can be used.
di
.autowired(true)
.setNameTransformer(function (name) {
return name.replace(/([A-Z])/g, function (str) {
return '-' + str.toLowerCase();
});
})
.register('diesel-engine')
.as(DieselEngine)
.register('tractor')
.as(Tractor);
di.resolve('tractor'); // { dieselEngine: { hp: 0 } }
An object which has method transform can be used.
var customNameTransformer = {
transform: function (name) {
return name.replace(/([A-Z])/g, function (str) {
return '-' + str.toLowerCase();
});
}
};
di
.autowired(true)
.setNameTransformer(customNameTransformer)
.register('diesel-engine')
.as(DieselEngine)
.register('tractor')
.as(Tractor);
di.resolve('tractor'); { dieselEngine: { hp: 0 } }
Returns default dependency resolver.
Allows to set default dependency resolver.
This flag allows to print an additional information to the output.
di.debug = true;
Sets dependency resolver to initial state. All registered types and instances are removed from it.
di
.register('dieselEngine')
.instance(new DieselEngine());
di.dispose();
di.resolve('dieselEngine'); // throws an exception
Returns module's version.
di.version; // 1.0.0
This software is distributed under the terms of the MIT License (MIT).
Gediminas Backevcius / @gedbac