0.0.3 • Published 5 years ago

injectbul v0.0.3

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

injectbul

Dependency Injection for NodeJS

npm.io

1. Introduction

injectbul helps you implement basic dependency injection to your pure nodejs code. Unlike other packages, you don't need to any framework.

Currently, it only supports Singleton lifestyle.

Project's primary target is helping anyone who used object oriented langues with dependency injection.

2. Dependencies

Project doesn't have any dependencies to any other packages.

3. Installation

npm i injectbul

4. Usage

injectbul requires an interface and an implementation of that interface.

Interface class should extends from iinterface class which comes with injectbul package.

Interface and implementation class should have identical methods.

Interface class method template should be as shown below:

var injectbul = require("injectbul")
class iistanbul extends injectbul.iinterface {
	eyup() {
		return this.instance.eyup()
	}
	cekmekoy() {
		return this.instance.cekmekoy()
	}
	sisli() {
		return this.instance.sisli()
	}
}
module.exports = iistanbul

Injections are defined in a config object. Config object template is as shown below:

var config = [
	{
		interfaceName: "iistanbul",
		interfacePath: "./services/cities/iistanbul",
		implementPath: "./services/cities/istanbul"
	},
	{
		interfaceName: "iedirne",
		interfacePath: "./services/cities/iedirne",
		implementPath: "./services/cities/edirne"
	}
]

These injections are stored in containers.container variable which comes with injectbul package.

Class definitions should contain a property with name dependencies. Implemented interfaces can be used as shown below:

class cityController{
    contstructor(){
        this.dependencies=["iistanbul", "iedirne"]
    }
    visitEyup(){
        return this.iistanbul.eyup()
    }
    visitUzunkopru(){
        return this.iedirne.uzunkopru()
    }
}

Whenever a class need an injection, it must be defined with a template below:

var injectbul = new injectbul.injectbul(config)
injectbul.inject(cityController)

Using a injected class should be called like shown below:

injectbul.get("cityController").visitEyup()
injectbul.get("cityController").visitUzunkopru()