Git Product home page Git Product logo

injector's Introduction

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

Installation

Execute this command in your environment.

npm install @alexey.kornilov/injector --save

or

yarn add @alexey.kornilov/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 "@alexey.kornilov/injector";

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

const injector = new Injector();
injector.register("foo", 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 "@alexey.kornilov/injector";

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

const injector = new Injector();
injector.register("foo", 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 "@alexey.kornilov/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

injector's People

Contributors

kavolorn avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.