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

dicon

v1.0.2

Published

A Dependency Injection Container for Node.js. Heavily inspired by AngularJS.

Downloads

10

Readme

dicon NPM version Build Status Dependency Status

Dependency Injection Container framework for Node.js

Why Dependency Injection ?

There are two things - Dependency Injection pattern (aka Inversion of Control) and Dependency Injection framework.

The Dependency Injection pattern is about separating the instantiation of objects from the actual logic and behavior that they encapsulate. This pattern has many benefits such as:

  • explicit dependencies - all dependencies are passed in as constructor arguments, which makes it easy to understand how particular object depends on the rest of the environment,
  • code reuse - such an object is much easier to reuse in other environments, because it is not coupled to a specific implementation of its dependencies,
  • and much easier to test, because testing is essentially about instantiating a single object without the rest of the environment.

Following this pattern is, of course, possible without any framework.

However, if you do follow the Dependency Injection pattern, you typically end up with some kind of nasty main() method, where you instantiate all the objects and wire them together. The Dependency Injection framework saves you from this boilerplate. It makes wiring the application declarative rather than imperative. Each component declares its dependencies and the framework does transitively resolve these dependencies...

Example

var Car = function(engine) {
  this.start = function() {
    engine.start();
  };
};

var createPetrolEngine = function(power) {
  return {
    start: function() {
      console.log('Starting engine with ' + power + 'hp');
    }
  };
};


// a module is just a plain JavaScript object
// it is a recipe for the container, how to instantiate stuff
var module = {
  // if an object asks for 'car', the container will call new Car(...) to produce it
  'car': ['type', Car],
  // if an object asks for 'engine', the container will call createPetrolEngine(...) to produce it
  'engine': ['factory', createPetrolEngine],
  // if an object asks for 'power', the container will give it number 1184
  'power': ['value', 1184] // probably Bugatti Veyron
};

var di = require('dicon');
var container = new di.Container(module);

container.invoke(function(car) {
  car.start();
});

For more examples, check out the tests. You can also check out Karma and its plugins for more complex examples.

Registering stuff

type(token, Constructor)

To produce the instance, Constructor will be called with new operator.

var module = {
  'engine': ['type', DieselEngine]
};

factory(token, factoryFn)

To produce the instance, factoryFn will be called (without any context) and its result will be used.

var module = {
  'engine': ['factory', createDieselEngine]
};

value(token, value)

Register the final value.

var module = {
  'power': ['value', 1184]
};

Annotation

The container looks up tokens based on argument names:

var Car = function(engine, license) {
  // will inject objects bound to 'engine' and 'license' tokens
};

You can also use comments:

var Car = function(/* engine */ e, /* x._weird */ x) {
  // will inject objects bound to 'engine' and 'x._weird' tokens
};

Sometimes it is helpful to inject only a specific property of some object:

var Engine = function(/* config.engine.power */ power) {
  // will inject 1184 (config.engine.power),
  // assuming there is no direct binding for 'config.engine.power' token
};

var module = {
  'config': ['value', {engine: {power: 1184}, other : {}}]
};

Differences to Angular's DI

  • no config/runtime phases (configuration happens by injecting a config object)
  • no global module register
  • no array annotations (comments annotations instead)
  • comment annotation
  • no decorators (maybe not yet?)
  • service -> type
  • child injectors
  • private modules

Made for Karma. Heavily influenced by AngularJS. Also inspired by Guice and Pico Container.

License

MIT © Yuan Tao