Skip to content

Lightweight dependency injection container for Node.js

License

Notifications You must be signed in to change notification settings

arkerone/tiny-container

Repository files navigation

tiny-container (:construction: WIP)

Lightweight dependency injection container for Node.js

Example

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')

About

Lightweight dependency injection container for Node.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published