Skip to content

OlegTar/javascript_di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Javascript dependency injection. Proof of concept.

I used decorators from ES2016 for implementing a dependency injection.

Usage

  1. call implement('<name of interface>').as(<concrete object>);

example: implement('arg1').as(new Injectable());

  1. declare @inject('<name of interface>') before your Class:
@inject('arg1');
class MyClass {
}

The first parameter of the constructor of MyClass will be injected by IOC

Example

Injectable.js

//a some class for the demonstration of the injection
export class Injectable {
    constructor(arg) {
    }

    method() {
        console.log('Injectable class\'s method');
    }
}

example.js

import { implement, inject } from '../src';//functions for a dependency injection
import { Injectable } from './injectable';//example class for the injection

implement('arg1').as(new Injectable());//register interface 'arg1' as a concrete object

@inject('arg1')//@inject('arg1', 'arg2') - name(s) of interface(s) for the injection to MyClass
class MyClass {
    constructor(arg, test) {//the constructor accepts an argument which will be replaced by IoC and some another argument
        arg.method();
        console.log(test)
    }
}


let c = new MyClass(1);//call constructor without injectable parameters

Output:

Injectable class's method
1

We called the constructor with only one parameter, but it was called with two parameters implicitly.

About

Concept of dependency injection for javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published