2.1.0 • Published 3 years ago

simpledi-node v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

SimpleDi

Build Status dependencies

This is a fork and complete rewrite of SimpleDi, a very simple dependency injector.

Intended for Node 10+ environments.

Features

  • Simple API
  • No dependencies
  • Fully tested
  • Helps you find unresolved dependencies with getResolvedDependencyCount

Installation

npm install simpledi-node

API

Create a SimpleDi instance

const SimpleDi = require('simpledi-node');
const di = new SimpleDi(); 

Add dependency with factory function

di.registerWithFactory(name, factoryFn, dependencies, overwrite)

di.registerWithFactoryOnce(name, factoryFn, dependencies, overwrite)

ParameterTypeDescription
namestringThe name of the dependency
factoryFnfunctionA function that gets called when di.get(name) is called
dependenciesstring[]optional array of dependency names. The dependencies will be resolved and passed to the factoryFn when di.get(name) is called.
overwritebooleanoptional This allows to explicitly overwrite dependencies

Add dependency with class constructor

di.registerWithNew(name, clazz, dependencies, overwrite)

di.registerWithNewOnce(name, clazz, dependencies, overwrite)

ParameterTypeDescription
namestringThe name of the dependency
clazzfunctionA class or constructor function that gets instantiated when di.get(name) is called
dependenciesstring[]optional array of dependency names. The dependencies will be resolved and passed to the constructor when di.get(name) is called.
overwritebooleanoptional This allows to explicitly overwrite dependencies

Example

const SimpleDi = require('simpledi-node');
const di = new SimpleDi();

function Engine(config) {
    this.hp = config.hp;
    this.maxSpeed = config.maxSpeed;
}

function Car(engine) {
    this.text = 'This car has ' + engine.hp + 'hp!';
}

di.registerWithNew('Engine', Engine, ['engineConfig']);
di.registerConstant('engineConfig', {
    hp: 120,
    maxSpeed: 200
});
di.registerWithNewOnce('Car', Car, ['Engine']);

let car = di.get('Car'); 

console.log(car.text); // This car has 120hp!

Add constant dependency

di.registerConstant(name, value, overwrite)

ParameterTypeDescription
namestringThe name of the dependency
valuemixedA fix value to be returned whenever di.get(name) is called
overwritebooleanoptional This allows to explicitly overwrite dependencies

di.registerContants(keyValueMap, keyPrefix, overwrite)

ParameterTypeDescription
keyValueMapMap<string,*> or ObjectAn map or pojo whose (own) keys will be used to register the respective value as a constant
prefixstringoptional prefix to precede the registered keys
overwritebooleanoptional This allows to explicitly overwrite dependencies
Example:
const CC = {foo: 1, bar: 2};
di.registerConstants(CC);
di.get('foo'); // returns 1
di.registerConstants(CC, 'tmp.');
di.get('tmp.bar'); // returns 2

Get a dependency

di.get(name[, arg1[, arg2[, ...]]])

Returns a previously registered dependency and resolves all dependencies.
Throws an Error if the dependency is not registered.

NameTypeDescription
namestringThe name of the dependency
arg1, arg2, ...mixedoptional arguments that will be passed to the dependency's factory function or constructor additionally to its dependencies
Example:
const fn = function() { return [...arguments] };
di.registerWithFactory('foo', fn, ['BAR']);
di.registerConstant('BAR', 111);
di.get('foo', 444, 555); // returns [111, 444, 555]

NOTE: For dependencies registered via registerWithNewOnce() or registerWithFactoryOnce(), additional get-arguments (arg1, arg2, ...) are allowed only in the first invocation of di.get(). An error is thrown otherwise.
You can suppress this error by calling di.setIgnoreArgsPassedToFinalValue(true).

Example for circular dependency (throws error):
di.registerWithNew('A', class{}, ['B']);
di.registerWithNew('B', class{}, ['C']);
di.registerWithNew('C', class{}, ['A']);

di.get('A'); // throws "Circular Dependency detected: A => B => C => A"

Inspecting usages of dependencies

di.getResolvedDependencyCount(): Map<string, number>

Returns a Map of dependency names onto the number the dependency got resolved in total, including both direct di.get() calls and transitive resolving.

// Example
console.warn(...di.getResolvedDependencyCount()); // [ 'foo', 3 ] [ 'bar', 1 ]

License

MIT