Dependency Injection library for Node.js.
The Spur Framework is a collection of commonly used Node.JS libraries used to create common application types with shared libraries.
Visit NPMJS.org for a full list of Spur Framework libraries >>
- Features
- What is inversion of control and why you should use it?
- Quick start
- API Reference
- Contributing
- License
- Dependency injection (IoC) inspired by AngularJS
- Auto injects folders
- Ability to merge injectors
- Ability to link injectors
- Makes testing super easy
- Ability to substitute dependencies in tests
- Resolution of dependencies by querying via regular expresion
- Clear error stack trace reporting
Inversion of Control (IoC) is also known as Dependency Injection (DI). IoC is a pattern in which objects define their external dependencies through constructor arguments or the use of a container factory. In short, the dependency is pushed to the class from the outside. All that means is that you shouldn't instantiate dependencies from inside the class.
Inversion of control is used to increase modularity of the program and make it extensible, and has applications in object-oriented programming and other programming paradigms.
It allows for the creation of cleaner and more modular code that is easier to develop, test and maintain:
- Single responsibility classes
- Easier mocking of objects for test fixtures
- Easier debugging in Node.js' async environment
$ npm install spur-ioc --save
Here is a quick example that sets up the definition of an injector, some dependencies and a startup script.
var spur = require("spur-ioc");
module.exports = function(){
// define a new injector
var ioc = spur.create("demo");
//register external dependencies or globals
ioc.registerDependencies({
"_" : require("underscore"),
"path" : require("path"),
"console" : console,
"nodeProcess" : process
});
// register folders in your project to be autoinjected
ioc.registerFolders(__dirname, [
"demo"
]);
return ioc;
}
Example of file that depends on an injectable dependency. This example shows the usage of underscore (_).
module.exports = function(_){
return _.map([1,2,3], function(num) {
return "Task " + num
});
}
This example injects Tasks and console dependencies, both previously defiled in the injector.
module.exports = function(Tasks, console){
return {
print:function(){
console.log(Tasks)
}
};
}
Example of how to create an instance of the injector and start the app by using one of its dependencies.
var injector = require("./injector");
injector().inject(function(TasksPrinter){
TasksPrinter.print();
});
While it is tempting to utilize the fat arrow syntax in this top declaration file like the example below, it will not be supported by spur-ioc. For more information, read issue #26. Instead use the recommended approach above. There isn't a compelling reason to add that additional support. If you use this style, it will break as the report in issue #26.
var injector = require("./injector");
injector().inject((TasksPrinter) => {
TasksPrinter.print();
});
Dependency injection really improves the ease of testing, removes reliance on global variables and allows you to intercept seams and make dependencies friendly.
The tests examples are in coffee-script because the syntax of coffee script makes this code much lighter and easier to read and maintain.
injector = require "../../lib/injector"
describe "TasksPrinter", ->
beforeEach ->
@mockConsole = {
logs:[],
log:()-> @logs.push(arguments)
}
#below we replace the console dependency silently
injector()
.addDependency("console", @mockConsole, true)
.inject (@TasksPrinter)=>
it "should exist", ->
expect(@TasksPrinter).to.exist
it "should greet correctly", ->
@TasksPrinter.print()
expect(@mockConsole.logs[0][0]).to.deep.equal [
"Task 1", "Task 2", "Task 3"
]
In order to illustrate how to use Spur IoC, we created sample apps in both Coffee-Script and JavaScript. We will be building out a more elaborate application sample, so please check back soon.
One of the great things about ioc is that you get real application dependency errors upfront at startup
module.exports = function(TaskZ, console){
//...
}
// Produces:
// ERROR Missing Dependency TaskZ in $$demo -> TasksPrinter -> TaskZ
module.exports = function(_, TasksPrinter){
//...
}
// Produces:
// ERROR Cyclic Dependency TasksPrinter in $$demo -> TasksPrinter -> Tasks -> TasksPrinter
Please send in pull requests and they will be reviewed in a timely manner. Please review this generic guide to submitting a good pull requests. The only things we ask in addition are the following:
- Please submit small pull requests
- Provide a good description of the changes
- Code changes must include tests
- Be nice to each other in comments. ๐
The majority of the settings are controlled using an EditorConfig configuration file. To use it please download a plugin for your editor of choice.
Lint source code by running npm run lint
.
To run the test suite, first install the dependancies, then run npm test
$ npm install
$ npm test
$ npm run dev