Created
May 19, 2020 09:08
-
-
Save prapats/24e5598fd471c06f686312c8bbbfc275 to your computer and use it in GitHub Desktop.
AdonisJS DI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use string' | |
class LorryDriver { | |
constructor(license) { | |
this.license = license | |
} | |
drive() { | |
console.log('Lorry driver driving.') | |
} | |
} | |
module.exports = LorryDriver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
class LorryDrivingLicense{ | |
show() { | |
console.log('Lorry Driving License.'); | |
} | |
} | |
module.exports = LorryDrivingLicense |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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