zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
276 stars 34 forks source link

Binding Arguments by Name or Type for defaults #175

Open zazoomauro opened 2 years ago

zazoomauro commented 2 years ago

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', 'manager@example.com')
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: 'manager@example.com'

            # 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,
  ) {}
}