slim-dependency-injector

A minimalistic dependency injector

Usage no npm install needed!

<script type="module">
  import slimDependencyInjector from 'https://cdn.skypack.dev/slim-dependency-injector';
</script>

README

Slim Dependency Injector

A very simplistic dependency injector for NodeJS servers.

How to use

npm install slim-dependency-injector

Then in your code, lets say you have 3 nested classes.

class ClassA {
    constructor() { this.number = 5; }
}

class ClassB {
    constructor(aInstance) { this.aInstance = aInstance; }
}

class ClassC {
    constructor(a, b) { this.aInstance = a; bInstance = b; }
}

To configure and use the dependency injection:

var dependencyInjection = require('slim-dependency-injection');

var builder = new dependencyInjection.DependencyBuilder();

builder.whenBuild(ClassB).inject(ClassA);
builder.whenBuild(ClassC).inject(ClassA, ClassB);

var cInstance = Container.get(ClassC);

cInstance.aInstance.number == 5; // true