di-lite.js
di-lite is designed to act as a minimalistic dependency injection container in Javascript. It has no dependency on any other framework such as CommonJs or RequireJs in contrary to some of other more heavy weight DI container implementation on the web.
Use Cases
di-lite was initially extracted from a Backbone.js based application. In this application, RequireJs was used to wire dependencies at module level. Each module could contain multiple views and models. We found using RequireJs to wire every individual view and model is a bit of overkill and verbose, however we still want the inversion of control to keep our code nicely isolated and testable. That's the reason behind the creation of this lightweight container implementation - to provide dependencies injection at sub-modular level.
That being said di-lite is designed to be used standalone there is no dependency on Backbone or RequireJs. Its a fully functional dependency injection container by itself while extremely lightweight and works well both in browser or node.
Install with npm
npm install di-lite
Install without npm
Download
How-to Guide
- Basic Wiring
- Wiring with Assignment
- Passive Dependency Resolution
- Singleton By Default
- Prototype Strategy
- Passing Constructor Arguments
- Cyclical Dependency
- Functional Object
- Lifecycle Hook
- Runtime Dependencies Override
- Create Your Own
Basic Wiring
var { ... thisdependencies = "b, c"; ...};var { ... thisdependencies = "c"; ...};var C = {...}; // create di contextvar ctx = di; // register a class with an unique namectx;ctx;ctx; // initialize di container so all singleton(default) objects will be wired at this stagectx; var instanceOfA = ctx;instaceOfAb === ctx; // trueinstaceOfAc === ctx; // true var instanceOfB = ctx;instanceOfBc === ctx; // true
Wiring with Assignment
By default dependencies are wired using their registered name respectively, however you can override this behavior by including an explicit assignment in dependency definition. You can also mix both explicit and implicit assignment in dependency definition.
var { ... thisdependencies = "bee=b, c"; // mix explicit and implicit assignment ...}; ctx; var instanceOfA = ctx;instaceOfAbee === ctx; // true - explicit assignmentinstaceOfAc === ctx; // true - implicit assignment
Passive Dependency Resolution
di-lite container resolves dependency passively on demand when get()
method is invoked. It only tries to resolve
dependency for this particular object thus only traverse its sub-dependency-tree. initialize()
call does resolve
all dependencies for all registered objects however it is actually optional (though recommended to guarantee).
ctx;ctx; // this triggers the dependency resolution for "a" alonectx; // this triggers the dependency resolution for everyone registered
Singleton By Default
All objects created and managed by di-lite.js container are by default singleton.
ctx === ctx; // true
Prototype Strategy
If you want container to create a new instance of your class each time you invoke get
method then you need
to configure your registration with di.strategy.proto
cache strategy.
ctx;ctx === ctx; // falsectx; // create can be used if you want to explicitly pass in a new parameter
Passing Constructor Arguments
You can pass arguments to constructor function by using the additional parameter in register
call.
ctx; // signle simple argumentctx; // signle object literal argumentctx; // multiple argument is passed in using an array
Cyclical Dependency
di-lite.js container supports solution of cyclical dependencies, so the following dependency relationship is valid.
var { ... thisdependencies = "b"; ...};var { ... thisdependencies = "a"; ...}; ctx;ctx; ctx; ctxb === ctx; // truectxa === ctx; // truectxba === ctx; // truectxab === ctx; // true
Functional Object
What if your are using functional object pattern and do not have a constructor function for your object? di-lite.js fully supports functional object pattern since we believe this is the best way to create javascript object anyway.
var { var that = {}; ... return that;}; ctx; // function chaining can be used to customize your object registrationctx ; ctx; ctx; // will return you a signleton instance of FuncObjectctx; // will return you a new instance of FuncObject each time
Lifecycle Hook
di-lite container provides a lifecycle hook callback ready
if the wired object provide a ready
function
then the callback will be executed once all dependencies are satisfied.
var MyView = BackboneView;
Runtime Dependencies Override
What if I need to provide different dependencies different instances of the same class?
ctx ; // dependencies specified here will take precedence...ctxbee === ctx; // true
Create Your Own
Sometimes you just don't have control over the object you want to wire or maybe you need to create the object and initialize before anything else is create or even registered. You can manually insert object into di-lite container and control the whole creation process this way.
ctxobjectBackbonehistory;...ctxstart; // you can use it since it is already created and initialized...ctx; // initialize the rest of the objects
How to build di-lite locally
Prerequisite modules
Make sure the following packages are installed on your machine
- node.js
- npm
- apache ant
Install dependencies
di-lite$ npm install
Build and Test
di-lite$ ./make
License
di-lite is an open source javascript library and licensed under Apache License v2.