Container API
The runtime container is the central part of the Injex framework. With the core decorators, you define and manage the modules and their dependencies.
Although Injex's API is minimal and easy to follow, there are a few things you should keep in mind while working with this fantastic framework.
A singleton class injectable name
When you create a singleton class using the @singleton()
decorator, a camelCased representation of the class name is used as its injectable name.
The name of the class is MailService (with a capital 'M'), but the injectable name will be mailService, so to inject this module, we should do:
File module exports
For Injex to run and scan all the modules inside your project files, you should export your classes this way:
note
You can have multiple classes defined and exported from the same file.
Module access in constructors
Since Injex handles circular dependencies for you, one caveat is that you can't access injectable dependencies from module class constructors. Injex offers the @init()
decorator to bypass this limitation.
Decorators
Since Injex API built using decorators, use need to turn on the experimentalDecorators
in your application's tsconfig.json
file.
API
The runtime container exposes a small API you can use to handle dependencies. To get the runtime container instance, you call the create()
method.
For example:
Properties
container.logger
Access the internal runtime container logger.
Methods: info(...args)
, debug(...args)
, warn(...args)
, error(...args)
Methods
container.bootstrap()
Bootstraps and initialize the runtime container and all the registered modules.
- Returns: The container instance
- Async:
true
Learn more about the bootstrap lifecycle.
container.getModuleDefinition(moduleNameOrType)
Get a module metadata and its instance/factory method.
The module
object inside the definition returned can be an instance of a class (if it's a singleton module) or a factory method to create its instance.
- Returns:
{ metadata, module }
- Async:
false
For example, if we have a module which defined this way:
You can access the modue in this way:
container.addModule(item)
Adds a decorated class into the runtime container.
- Returns: The container instance
- Async:
false
- Arguments:
item
- A decorated module class
For example:
container.addObject(obj, name)
Adds an object into the runtime container.
- Returns: The container instance
- Async:
false
- Arguments:
obj
- the object you wish to addname
- the injection name of the object
For example:
container.removeObject(name)
Removes an object from the runtime container
- Returns: The container instance
- Async:
false
- Arguments:
name
- the injection name of the object
container.get(itemNameOrType)
Similar to the @inject()
decorator, get a module from the runtime container, which can be an instance of a class (if it's a singleton module) or a factory method to create its instance.
container.getAlias(alias, keyBy)
Similar to the @injectAlias(NAME, KEY_BY)
decorator, get an alias map from the runtime container.