Container
Index
Constructors
constructor
Returns Container
Methods
staticget
Container.get()
allows to get an instance of a class using its factory.Type parameters
- T
Parameters
type: string | Constructable<T>
The type or name of the class to get.
optionaloptions: { args?: unknown[]; metadata?: {} }
Allows selection of a specific factory to use to get the instance.
- The
args
option supplies arguments to the factory. - The
metadata
option identifies the factory.
- The
Returns Promise<T>
The instance of the class.
staticregisterFactory
Container.registerFactory()
allows to register a factory for a class.Type parameters
- T
Parameters
type: string | Constructable<T>
The type or name of the class for which the factory is registered.
factory: Factory<T>
The factory class being registered.
optionaloptions: { metadata?: {} }
Distinguishes between different factories of the same class.
- The
metadata
attaches custom metadata to the factory.
- The
Returns void
staticreset
Container.reset()
clears all registered factories and empties the container. This is mostly used in testing.Returns void
staticsetDefault
Container.setDefault()
sets a default factory for the class.Type parameters
- T
Parameters
type: string | Constructable<T>
The type or name of the class for which the default factory is set.
factory: Factory<T>
The factory class being set as default.
Returns void
staticsetFactoryTimeout
The Container, by default, will wait a maximum of 5 seconds for a factory to resolve and provide an instance. This method allows to change that timeout.
Parameters
timeoutInMs: number
The timeout in milliseconds.
Returns void
staticunRegisterFactory
Container.unRegisterFactory()
will unregister all factories of a class.Type parameters
- T
Parameters
type: string | Constructable<T>
The type or name of the class for which all factory is unregistered.
Returns void
The Container class is a box to hold all registered factories.
In Octo, we minimize the use of the
new
keyword and get instance of a class using its factory. The factory has full control over how the instance is created.By using the Container and Factory concepts, it is possible to override internal class definitions at runtime. It is an incredibly powerful tool to customize implementation.