node-dependency-injection icon indicating copy to clipboard operation
node-dependency-injection copied to clipboard

Binding Arguments by Name or Type for defaults

Open zazoomauro opened this issue 3 years ago • 0 comments

Bind keyword to bind specific arguments by name or type:

or in pure js or typescript

const compiler = new ContainerBuilder(false, '../path/to/src')
compiler.addBind('adminEmail', '[email protected]')
compiler.addBind('someParameter', process.env.SOME_PARAMETER)
const autowire = new Autowire(compiler)

or in yaml

services:
    _defaults:
        bind:
            # pass this value to any adminEmail argument for any service
            # that's defined in this file (including controller arguments)
            adminEmail: '[email protected]'

            # also if is coming from environment variables
            someParameter: '%env(SOME_PARAMETER)%'

Then you can use it it that way

export default class SomeService() {
  constructor(
    private readonly adminEmail: string,
    private readonly someParameter: string,
  ) {}
}

zazoomauro avatar May 20 '22 07:05 zazoomauro