Core: Ioc in app/services instead providers/

Created on 16 May 2018  路  6Comments  路  Source: adonisjs/core

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()
    }
}

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

const Config = use('Config')

class HelloService {
}

module.exports = new HelloService()

All 6 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PC-HUB picture PC-HUB  路  4Comments

dezashibi picture dezashibi  路  4Comments

milosdakic picture milosdakic  路  3Comments

umaams picture umaams  路  3Comments

Extarys picture Extarys  路  4Comments