0.7.1 • Published 7 years ago
core-di v0.7.1
core-di
Simple and minimal dependency injection (DI) for ES6
Installation
npm install core-di --save
Quickstart
Giving the following (note that the use of ES7 decorators here is not mandatory )...
car.js
import GearBox from './gear-box'
import SteeringWheel from './steering-wheel'
import {inject} from 'core-di'
@inject(
GearBox,
SteeringWheel
)
export default class Car {
constructor (geerBox, steeringWheel) {
this.geerBox = geerBox
this.steeringWheel = steeringWheel
}
}
You can then setup the di container as follows...
container.js
import Car from './car'
import GearBox from './gear-box'
import SteeringWheel from './steering-wheel'
import {Container} from 'core-di'
const container = new Container()
container.add(Car)
container.add(GeerBox)
container.add(SteeringWheel)
export default container
At which point you can then use the container to construct an item with all its dependencies resolved like so...
main.js
import Car from './car'
import container from './container'
const car = container.resolve(Car)
console.log(car instanceof Car) // -> true
console.log(car.geerBox instanceof GeerBox) // -> true
console.log(car.steeringWheel instanceof steeringWheel) // -> true
more docs to come...