Razor thin and unobtrusive dependency injection
npm install dinWhen you are dealing with large code bases in node.js which is common place as small applications grow very large, the code
base increasingly difficult to manage, update and test.
Using require to load modules directly makes it difficult to control what dependencies your modules use and when it comes time for testing or swapping out implementations of dependencies, it becomes difficult without having to change the module internally or hack the require cache.
This library aims to be as thin as possible as well as completley unobtrusive to the modules themselevs, as opposed to most DI libraries which require modules are written in a specific way or depend on a global variable such as 'define'.
The following example shows how to load multiple dependencies in many supported usage scenarios.
#### Example module - lib/testModule.js
``javscript`
module.exports = function(normalModule, computed, inlineFunction, string, number) {
console.log(arguments);
}
#### App module wiring config - wiring.js
`javscript`
module.exports = {
baseDir : __dirname, //alternative to relative path names
graph : {
'modulea' :{ // key used to load module
lookup:'lib/testModule', //actual name of js file
deps:[ // array containing dependencies required to load module
console.log, // injects the console.log function
function() {console.log('inlineFunctionDep');}, // functions can be inlined as dependencies
's:stringDep', // s dentoes a string literal
123456, // numbers are passed straight through
'libAnotherTestModule', // loads a module refernced in the wiring
'_din' // injects the instance of din to allow for modules to be loaded on demand
]
}
}
};`
#### Load DI - app.jsjavscript
var Din = require('../din'),
appWiring = require('./wiring'), // config
din = Din(appWiring),
modulea = din.load('modulea');
``