The app/services folder is autoloaded by default. But is it possible to bind my services within this folder using IoC, so I do not need to use 'new' to instantiate my class and not use ServiceProvider?
Example:
// app/services/HelloService.js
class HelloService {
constructor (Config) {
this.Config = Config
}
foo () {
return 'foo'
}
}
module.exports = HelloService
// app/controllers/HelloController.js
const HelloService = use('app/services/HelloService')
class HelloController {
bar () {
const foo = HelloService.foo()
}
}
Simply export new HelloService()?
In this case I should pass the configuration reference, right?
const { ioc } = require('@adonisjs/fold')
const Config = ioc.use('Adonis/Src/Config')
module.exports = new HelloService(Config)
Once your code is inside your own Adonis app, there is no need to complex things. The real benefit of IoC bindings is, when you want to distribute the code.
Keep it as simple as
const Config = use('Config')
class HelloService {
}
module.exports = new HelloService()
Thank you, @thetutlage.
By the way, every day I love AdonisJS more, congratulations.
Thanks 馃槃
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Once your code is inside your own Adonis app, there is no need to complex things. The real benefit of IoC bindings is, when you want to distribute the code.
Keep it as simple as