injct

dependency injection for nodejs

npm install injct --save
var injct = require('injct');
 
function UserRepository() {}
injct.register('userRepository', UserRepository);
 
function UserService(userRepository) {
    this.userRepository = userRepository;
    injct.apply(this);
}
 
var userService = new UserService();
assert.equal(userService.userRepository instanceof UserRepository);
  • prototype: new instance when the class is requested
injct.register(name, class)
  • unique: reuse the instance
injct.unique(name, class)

Say you have a registered UserRepository class like this:

var injct = require('injct');
injct.register('userRepository', UserRepository);
 
function UserRepository() {}
module.exports = UserRepository;

And an user service that expects that UserRepository to be injected

function UserService(userRepository) {
    this.userRepository = userRepository;
    injct.apply(this);
}

You can mock the repository behavior, no matter the scope

injct.register('userRepository', function AnotherRepository(){});
 
var userService = new UserService();
assert.ok(userService.userRepository instanceof AnotherRepository);