ghost-inject
v0.1.1
Published
Dependency injection (DI) management tool
Downloads
8
Maintainers
Readme
Ghost is simple dependency injection manager. It is easy to use and avaliable in node.js and browser. No dependencies are needed.
Installation
$ npm install ghost-inject
Usage
Initialization
Ghost = require("ghost-inject")
injector = new Ghost();
Adding services
anyService = {
a: 10
};
injector.addService("any", anyService);
Adding factories
Factories are lazy loaded services. Once the service is created by the factory, it is saved and returned each time.
databaseFactory = function() {
return {
connected: true
};
};
injector.addFactory("database", databaseFactory);
Calling functions with dependencies
func = function(database) {
// database is injected here if service or factory was provided before
};
injector.call(func)
Injecting functions
func = function(database) {
// database is injected here if service or factory was provided before
};
// injector will return function with bound arguments
bound = injector.inject(func);
bound(); // call injected function
both
call
andinject
can be provided with this parameter as a second parameter. Also additional dependency list can be provided for local dependencies.
if collision occurs within specified list and service in the injector, service from the list will be used
Creating instances
car = function(people) {
this.people = people;
this.handbreak = true;
};
myCar = injector.create(car);
people
argument will here be injected into constructor if service calledpeople
was previously defined
Resolving dependencies
func = function(firstdep, seconddep) {};
deps = injector.resolve(func);
// deps is now array containing arguments for the given function
Getting function arguments
func = function(a, b, c) {};
deps = injector.getArguments(func)
// deps = ['a', 'b', 'c']