Skip to content

Instantly share code, notes, and snippets.

@prapats
Created May 19, 2020 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prapats/24e5598fd471c06f686312c8bbbfc275 to your computer and use it in GitHub Desktop.
Save prapats/24e5598fd471c06f686312c8bbbfc275 to your computer and use it in GitHub Desktop.
AdonisJS DI
'use strict'
class Car {
constructor(driver) {
this.driver = driver
}
run() {
this.driver.drive()
console.log('Car running');
}
static get inject() {
return ['App/Services/TaxiDriver']
}
}
module.exports = Car
'use string'
class LorryDriver {
constructor(license) {
this.license = license
}
drive() {
console.log('Lorry driver driving.')
}
}
module.exports = LorryDriver
'use strict'
class LorryDrivingLicense{
show() {
console.log('Lorry Driving License.');
}
}
module.exports = LorryDrivingLicense
// Calling in controller (Recursively resolving dependecies)
const car = make('App/Services/Car')
// When testing
// const LorryDrivingLicense = use('App/Services/LorryDrivingLicense');
// const TaxiDriver = use('App/Services/TaxiDriver');
// const Car = use('App/Services/Car')
const car = new Car(new TaxiDriver(new LorryDrivingLicense));
car.run()
'use string'
class TaxiDriver {
constructor(license) {
this.license = license
}
drive() {
this.license.show()
console.log('Taxi driver driving.')
}
static get inject(){
return ['App/Services/TaxiDrivingLicense'];
}
}
module.exports = TaxiDriver
'use strict'
class TaxiDrivingLicense{
show() {
console.log('Showing taxi driving license.');
}
}
module.exports = TaxiDrivingLicense
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment