0.1.0 • Published 7 years ago

componafide v0.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

IOC Container for NodeJS but with Prospects

NodeJS got prospects. He's componafide.

Usage

const container = module();

`container.register(name, dependencies, generator);

container.register('myComponent', ['dep1', 'dep2'], (dep1, dep2) => { /* return myComponent */ });
container.register('dep1', [], () => { /* return dep1 */ });

container.constant(name, value);

Convienience for container.register(name, [], () => value);.

const component = container.resolve(name);

const myComponent = container.resolve('myComponent');

Usage Pattern

A Component Per File

Export a component in each file. Put all dependencies in closure, and return an API or value. Require nothing outside of a file or two. No regrets. Go state!

module.exports = (dep1, dep2) => {

  function myPrivateMethod () {}

  return class MySingleton {
    static myPublicMethod () {}
  }

}
container.register('myComponent', require('src/myComponent'), ['dep1', 'dep2']);
const component = container.resolve('myComponent');

Singletons

module.exports = dep1 => {

  function privateMethod () {}

  return class SingletonAPI {
    static publicMethod (arg1, arg2) {}
  }

}

Classes

module.exports = Pomade => {

  function applyTheDap () {}

  return class DapperDan extends Pomade {
    apply () {}
  }

}

Constants, Native, and Global Gimmes

Still use dependency injection. If it feels stupid your are probably still doing it right.

container.constant('scope.process', process);
container.constant('pi', 3.14); // Because you might want to test what happens to your code when the universe implodes and pi changes to 23.