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

no-configuration-di

v0.3.3

Published

A simple, configuration-free dependency injection framework

Downloads

826

Readme

A simple, configuration-free dependency injection framework

Given a bunch of classes of the form

Namer.js:

module.exports = function() {
    this.getName = function() {
       return 'World';
    }
}
Greeter.js:

module.exports = function(namer) {
    this.great = function() {
        console.log('Hello %s', namer.getName());
    }
}

This module allows you to start them all simply by

var Di = require('no-configuration-di');
var di = new Di(__dirname + '/src');

di.load('Namer');
var greeter = di.load('Greeter');
greeter.greet();

To make this work the following rules have to be followed:

  • The filename is the class name and should be CamelCased. Instances will be lowerCameCased. FooBar.js will instanciate as an instance with the name fooBar
  • Files can be nested in folders, but only the actual filename counts as class name. db/UserDao.js needs to be instanciated as di.load('db/UserDao') but will be injected in all classes asking for userDao
  • The only place to define your dependencies for a certain class is in the parameter list of the constructor function
  • load() has to be called in the right order.
  • This libary doesn't supports lazy loading

API

load(path, [instanceName])

Loads a class, resolves dependencies and initialises it. If no instanceName is given it will be implied from the filename (db/Query would become an instance of query)

di.load('db/Query', 'userQuery');

add(instance, instanceName)

Adds an instance of an object

di.add('config', {port: 1234});

get(instanceName)

Retrieves an instance of a given name. Returns undefined if nothing for that name is found.

app.listen(di.get('config').port);

runAll(functionName)

Checks all instances for the existence of a function called functionName and runs them in the order they have been imported in. Asynchronous functions are supported via https://www.npmjs.org/package/q . Should any of them throw an error no other function will be called.

di.load('DatabaseManager');
di.add('HttpServer', {
  start: function() {
    var deferred = Q.defer();
    server.listen(8080, deferred.makeNodeResolver());
    return deferred.promise;
  }
});
di.runAll('start')
.done(function() {
  console.log('Database and Server have been successfully started');
});

loadDecorator(path, [decoratorName])

See chapter about "Decorators". path has to be a path to a function, decorators won't be "newed", but just called. Decorator can have multiple dependencies, but the first parameter has to be the subject which is going to be decorated. No return value is expected, the subject should be decorated in place.

if no decoratorName is defined it will be implied from the filename: loadDecorator('decorators/UserDbAccess.js') will add an instance of userDbAccessDecorator that can be now be injected in other classes.

If multiple decorators share the same decoratorName those decorators will be chained.

Decorators

Decorators are useful for turning simple objects into objects with methods. Here's an example that uses multiple decorators chained together:

Advertiser.js:

module.exports = function(userDecorator) {

    this.advertiseToUser = function(userId) {
        database.getUserById(userId)
        .then(function(user) {
            userDecorator.decorate(user);
            user.sendEmail('Please come back to our site, your friend ' + user.getRandomFriend() + " is missing you.");
        });
    };
}
decorators/UserEmail.js:

module.exports = function(user, emailer) {
    user.sendEmail = function(text) {
        emailer.send(user.email, text);
    }
}
decorators/UserFriends.js:

module.exports = function(user) {
    user.getRandomFriend = function() {
        return 'Fred'
    }
}
app.js

di.load('Database');
di.load('Emailer');
di.loadDecorator('decorators/UserEmail', 'userDecorator');
di.loadDecorator('decorators/UserFriends', 'userDecorator');
di.load('Advertiser');

di.get('advertiser').advertiseToUser(14);