-
Notifications
You must be signed in to change notification settings - Fork 34
WorkingWithDefinitions
Mauro edited this page May 27, 2022
·
7 revisions
You may sometimes want to use definitions to build your container or to modify the container behaviour, for example in compiler pass process methods.
import {ContainerBuilder, Definition} from 'node-dependency-injection'
let container = new ContainerBuilder()
let definition = new Definition(SomeClass)
container.setDefinition('some_manager', definition)
This is a shortcut of
import {ContainerBuilder, Definition} from 'node-dependency-injection'
let container = new ContainerBuilder()
container.register('some_manager', SomeClass)
You can also send arguments in the definition constructor like:
import {ContainerBuilder, Definition, PackageReference} from 'node-dependency-injection'
let container = new ContainerBuilder()
let definition = new Definition(SomeClass, [
'some_string_dependency',
new PackageReference('some-npm-package')
])
container.setDefinition('some_manager', definition)
If the service you are working with uses setter injection then you can manipulate any method calls in the definitions as well:
definition.addMethodCall('setLogger', [new Reference('logger')]);
definition.public = false
definition.setFactory(SomeManager, 'getFactory')
definition.deprecated = 'Example of message deprecation'
definition.lazy = true
definition.addTag('tag_name')
definition.addProperty('property_name', new Reference('app.some_manager'))
-
findTaggedServiceIds(string tagName): Iterable
get a list of tagged services Definitions
-
hasDefinition(string serviceName): boolean
find out if there is an serviceName definition
-
has(string serviceName): boolean
find out if there is an serviceName definition or alias
-
getDefinition(string serviceName): Definition
get the serviceName definition
-
findDefinition(string serviceName): Promise
get the definition with the serviceName ID or alias
-
setDefinition(Definition definition): Definition
sets a definition to register in to the container
-
removeDefinition(string serviceName): boolean
removes an already set definition
Copyright © 2023-2024 Mauro Gadaleta