Skip to content

A simple dependency injection container to help implement IoC in Javascript

Notifications You must be signed in to change notification settings

techmo-io/js-di-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-di-container

A simple yet adequately powerful dependency injection container to help implement IoC in Javascript. It supports registering/binding:

  • classes
  • function constructors
  • factory functions

Auto sub-dependency resolution is supported.
Services can be resolved as singletons or new instances each time they are requested by passing an "options" parameter to "register*" methods.

Installation

npm install --save js-di-container

Examples

Simple service registeration using classes and a factory function.By default all services are singletons unless explicitly set otherwise using {singleton: false} option :

class Redis {
}
let ConfigClass = function(){
  this.field = 'something'
}
function databaseFactory(container){
  class DB{}
  return new DB();
}

let container = new Container();
container.registerClass('cache', Redis);
container.registerClass('cacheNonSingleton', Redis, {singleton: false});
container.registerClass('config', ConfigClass);
container.registerFactory('db', databaseFactory);
//somewhere in your code
let redis = container.get('cache')

Automatic sub-dependency resolution from function/constructor parameter names.You can list service dependencies by their names as parameters of class constructor or factory function and they will be resolved automatically for you:

class UserRepository {
  constructor(store){ //can have more than one dependency
    this.dataStore = store;
  }
}
class Database {}
let container = new Container()
container.registerClass('store', Database);
container.registerClass('users', UserRepository);
let usersRepo = container.get('users'); //store sub-dependency will be injected automatically

Also note that factory functions that are passed to 'registerFactory' method receive the container object as their first argument along with other dependencies listed as parameters to the factory function after the first argument (i.e. container; the name doesn't matter for the first parameter):

class Time {}
class Logger {}
let config = {key: 'secret'}

function loggerFactory(container, time){
 return new Logger(time, config.key)
}

let container = new Container();
container.registerClass('timer', Time);
container.registerFactory('logger', loggerFactory, {singleton: false})
let newLogger = container.get('logger') 

About

A simple dependency injection container to help implement IoC in Javascript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published