Lightweight dependency injection container for Node.js
const tinyContainer = require('tiny-container')
const container = tinyContainer.createContainer()
class Logger {
constructor () {
console.log('Instantiate Logger')
}
}
class DbManager {
constructor ({ logger }) {
console.log('Instantiate DbManager with logger')
this.logger = logger
}
}
class UserService {
constructor ({ logger, dbManager }) {
console.log('Instantiate UserService with logger and DbManager')
this.logger = logger
this.dbManager = dbManager
}
}
container.register('logger', Logger)
container.register('dbManager', DbManager)
container.register('userService', UserService)
/* We get our userService service ...
* Our container instantiates all the services that userService needs
* and automatically takes care of the injection:
* Instantiate Logger
* Instantiate DbManager with logger
* Instantiate UserService with logger and DbManager
*/
const userService = container.resolve('userService')