npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-di-container

v0.1.2

Published

Simple Dependency Injection Container

Downloads

9

Readme

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!