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

areus-di

v0.5.1

Published

Areus Dependency Injector

Downloads

13

Readme

Dependency Injector Build Status NPM version

Here's an example of a module that has two parts: a service and a db. The service depends on the db.

Here's the db.js:

module.exports = Db;

function Db() {
}

Db.$inject = [];

Db.prototype.answer = function () {
  return 42;
};

And here's service.js:

module.exports = Service;

function Service(db) {
  this._db = db;
}

Service.$inject = [
  './db'
];

Service.prototype.answer = function () {
  return this._db.answer();
};

Note how the service has one special property $inject. It is an array of paths of modules that are to be "injected". Use this just as you use require(). The exact same rules that Node.js uses for resolving the modules apply.

The only difference is that if the $injected module is a "class" (function) which has an $inject property that is an array, a new instance of that class will be created and provided as an argument of the constructor of the class that $injected it. This happens recursively. Thus, if the injected class had an $inject of its own, that would be resolved first.

For example, in this case the Db has no dependencies to inject, so a new instance of the Db class will be created and provided as the first argument in Service's constructor.

Finally, here's index.js that gets a fresh new instance of the Service class.

var DI = require('areus-di'),
  di = DI(__dirname);

exports.create = function () {
  return di.create('./service');
};

The __dirname is the root directory, relative to which ./service is locataed.

Providing Existing Packages

Use the .provide() method to provide existing packages. Any package that you give to .provide() will be available in $inject. Here's an example:

index.js

var DI = require('areus-di'),
  mongo = require('mongoskin'),
  di = DI(__dirname);

di.provide({
  db: mongo.db(process.env.MONGO_URI)
});

di.create('./article_service');

article_service.js

module.exports = ArticleService;

function ArticleService(db) {
  this._articles = db.collection('articles');
}

ArticleService.$inject = [
  'db'
];

ArticleService.prototype.findOne = function (id, cb) {
  this._articles.findOne(id, cb);
};

Why Use a Dependency Injector?

  • Isolation In the example above Service did not need to know how Db was created. It did not have to be aware of the dependencies of Db or care whether it was backed by LevelDB, MongoDB or PostgreSQL. This is important because this implies that Db can change independently from Service.

  • Testability to test Service a new instance can be created with a fake Db as the first argument. For example:

var Service = require('../service'),
  assert = require('assert'),
  fakeDb, service;

fakeDb = {
  answer: function () {
    return 7;
  }
};

service = new Service(fakeDb);
assert.equal(7, service.answer());

Guiding Principles

  • No new knowledge needed for basic operation.

    • Path resolution follows the require() algorithm to a T.
  • In addition to require(), $inject instantiates the "class".

License

MIT