GithubHelp home page GithubHelp logo

simple-di-container's Introduction

simple-di-container

Simple Dependency Injection Container for Node.js

Simple DI Container is an easy-to-use Dependency Injection container for Node.js inspired by SimpleInjector

Installation

$ npm install simple-di-container

Usage

Below is an example that shows and explains how Simple Dependency Injection Container should be used.

Please note the 'register' and 'registerSingle' methods - which allows you to support both singleton and new instances:

  • registerSingle: creates a single instance of the module for the whole lifetime of your application
  • register: creates and returns a new instance of the module for each call to the 'get' method

Given the following 3 modules:

module.exports = function() {
  var obj = {};
  obj.name = "depA";
  return obj;
};
module.exports = function() {
  var obj = {};
  obj.name = "depA";
  return obj;
};
module.exports = function(depA, val1) {
  var obj = {};
  obj.depA = depA;
  obj.val1 = val1;
  obj.name = "depB";
  return obj;
};
module.exports = function(depA, depB, strVal) {
  var obj = {};
  obj.depA = depA;
  obj.depB = depB;
  obj.strVal = strVal;
  obj.name = "depC";
  return obj;
};

When you register your dependencies:

// Get a reference to the DI Container:
var diContainer = require('simple-di-container').diContainer;

// Get your dependencies:
var depA = require('./depA');
var depB = require('./depB');
var depC = require('./depC');

// Register your dependencies:

// Use 'registerSingle' for singleton dependencies:
diContainer.registerSingle("depA", depA);

// Use 'register' to get a new instance of your dependencies:
diContainer.register("depB", depB);

diContainer.registerSingle("depC", depC);
diContainer.register("strVal", "some value");
diContainer.registerSingle("val1", 1);

// After registering all your dependencies, call the 'verify' method.
// 'verify' throws an exception for circular dependencies or for missing dependencies
diContainer.verify();

Then you should easily be able to get the desired instance:

// Gets singleton instance for depA
var depA = diContainer.get("depA");
// Gets new instance of depB, for each call to diContainer.get("depB")
var depB = diContainer.get("depB");
var depC = diContainer.get("depC");
var strVal = diContainer.get("strVal");
var val1 = diContainer.get("val1");

et voilà - it's really that simple... and easy!

simple-di-container's People

Contributors

alonna avatar interal avatar vklap avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.