@brainstaff/injector

Dependency Injector for JavaScript projects.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@brainstaff/injector
1102.0.04 years ago6 years agoMinified + gzip package size for @brainstaff/injector in KB

Readme

Injector
npm version license
This is dependency injector for JavaScript with support for ES6/ES2015. It can be applied in both node and browser environments.

Table of Contents

- [Constructor Registration](#constructor-registration)
- [Object Registration](#object-registration)
- [Dependency Injection](#dependency-injection)

Installation

Execute this command in your environment.
npm install @brainstaff/injector --save
or
yarn add @brainstaff/injector

Table of Contents

Usage

Constructor Registration

One is able to register constructor for a particular class and create instances of it:
import Injector from "@brainstaff/injector";
import { asClass } from "@brainstaff/injector";

class Foo {
    constructor() {
        console.log("Creating instance of Foo.");
    }
    
    get dependencies() {
        return [];
    }
}

const injector = new Injector();
injector.register("foo", asClass(Foo));

// Resolve class by its registered name
const foo1 = injector.resolve("foo");

// Resolve class by its constructor reference
const foo2 = injector.resolve(Foo);

Expected output will be:
[Tue Mar 28 2017 08:39:53 GMT+0300 (MSK)] [Injector] Registering 'foo' component.
Creating instance of Foo.
Creating instance of Foo.

Table of Contents

Object Registration

One is able to register pure object.
import Injector from "@brainstaff/injector";
import { asValue } from "@brainstaff/injector";

const foo = {
    name: "Foo object " + Math.round(Math.random() * 10)
};

const injector = new Injector();
injector.register("foo", asValue(foo));

// Resolve registered object
const foo1 = injector.resolve("foo");
console.log(foo1);

// Resolve registered object
const foo2 = injector.resolve("foo");
console.log(foo2);

// We are expecting same object
console.log(foo1 === foo2 ? 
    "Same object resolved." : 
    "WARNING: unexpected resolved object.")

Expected output will be:
[Tue Mar 28 2017 08:46:31 GMT+0300 (MSK)] [Injector] Registering 'foo' component.
{ name: 'Foo object 5' }
{ name: 'Foo object 5' }
Same object resolved.

Table of Contents

Dependency Injection

Now it's time to demonstrate dependency injection.
import Injector from "@brainstaff/injector";

class Foo {
    constructor() {
        console.log("Creating instance of Foo.");
    }
    
    get dependencies() {
        return [];
    }
}

class Bar {
    constructor(dependencies) {
        console.log("Creating instance of Bar.");
        Object.assign(this, dependencies);
    }
    
    get dependencies() {
        return [
            "foo"
        ];
    }
}

const injector = new Injector();
injector.register("foo", Foo);
injector.register("bar", Bar);

// Resolve class by its registered name
const bar1 = injector.resolve("bar");

// Bar contains injected foo
console.log(bar1);

// Resolve class by its constructor reference
const bar2 = injector.resolve(Bar);

// Bar contains injected foo
console.log(bar2);

Expected output will be:
[Wed Mar 29 2017 15:27:08 GMT+0300 (MSK)] [Injector] Registering 'foo' component.
[Wed Mar 29 2017 15:27:08 GMT+0300 (MSK)] [Injector] Registering 'bar' component.
Creating instance of Foo.
Creating instance of Bar.
Bar { foo: Foo {} }
Creating instance of Foo.
Creating instance of Bar.
Bar { foo: Foo {} }

Also, usage examples can be found in ./examples folder.
Table of Contents

Changelog

v2.0.0

  • Introduced definitions (asValue, asClass, asFunction).
  • Switched to rollup from webpack.

v1.0.0

  • Initial injector implementation.

Table of Contents