injecorator

1.0.0 • Public • Published

Injecorator

A simple decorater based dependecy injection framework for nodejs es6 projects

NPM version Build Status

Installation

$ npm install injecorator --save

Usage

usage requiring babel-plugin-transform-decorators-legacy with babel 6

import {Inject, IocContainer } from 'injecorator';
 
const staticObject = { staticdep: true };
 
@Inject(staticObject)
class One
{
    constructor(staticOb){
        this.static = staticOb;
        this.isReallyOne = true;
    }
}
 
@Inject(staticObject, One)
class Two
{
    constructor(staticOb, oneCls){
        this.static = staticOb;
        this.one = oneCls;
        this.isReallyTwo = true;
    }    
}
 
const ioc = new IocContainer();
ioc.regAll(staticObject, One, Two);
const two = ioc.get(Two);
console.log(two);

Without using javascript decorators set a static property $inject on the object that contains an array of dependencies

class Three
{
    constructor(two){
        this.two = two;
        this.isReallyThree = true;
    }     
}
Three.$inject = [Two];

Registering an extension or replacement depency

const ioc = new IocContainer();
ioc.regAll(staticObject, Two);
ioc.register(One, { isReallyOne: false });
const two = ioc.get(Two);
expect(two.one.isReallyOne).to.be.equal(false);

Registering a factory method as a value provider. The argument passed to this method is an instance of the ioc container

class OneDerived extends One
{
    constructor(staticOb){
        super(staticOb);
        this.isDerivedOne = true;
    }
}
 
const ioc = new IocContainer();
ioc.register(OneDerived);
ioc.register(One, (iocArg) => iocArg.get(OneDerived));
 
const one = ioc.get(One);
expect(one.isReallyOne && one.isDerivedOne).to.be.equal(true);

Package Sidebar

Install

npm i injecorator

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • raudsley-ot
  • vmitrevski