Container
Index
Methods
get
To get an instance of a class from its factory. If the factory is not registered yet, this waits for it, and times out - see Container.setFactoryTimeout.
Type parameters
- T
- F: Factory<T> = never
Parameters
type: string | Constructable<T>
The type or name of the class to get.
optionaloptions: { args?: Parameters<F[create]>; metadata?: {} }
Allows selection of a specific factory to use to get the instance.
- The
argsoption supplies arguments to the factory. - The
metadataoption identifies the factory.
- The
optionalargs: Parameters<F[create]>
optionalmetadata: {}
Returns Promise<T>
The instance of the class.
has
Whether a factory is registered for the class.
Type parameters
- T
Parameters
type: string | Constructable<T>
optionaloptions: { metadata?: {} }
optionalmetadata: {}
Returns boolean
registerFactory
To register a factory for a class. Prefer the Factory decorator; call this directly only when the factory is built at runtime.
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
metadataattaches custom metadata to the factory.
- The
optionalmetadata: {}
Returns void
registerValue
To register a ready-made instance, instead of a factory that builds one.
Type parameters
- T
Parameters
type: string | Constructable<T>
value: T
optionaloptions: { metadata?: {} }
optionalmetadata: {}
Returns void
replaceValue
To register a ready-made instance over whatever is already registered under the same type and metadata, rather than refusing as Container.registerValue does.
Registering once is the right default - two factories for one type is usually a mistake. This is for the callers that legitimately register the same thing more than once: a module whose
onInitruns on everycompose(), and a test replacing a factory with a mock.Type parameters
- T
Parameters
type: string | Constructable<T>
value: T
optionaloptions: { metadata?: {} }
optionalmetadata: {}
Returns void
reset
To clear every registered factory and empty the container. Mostly used in testing.
Returns void
setFactoryTimeout
To change how long
get()waits for a factory to be registered.Parameters
timeoutInMs: number
The timeout in milliseconds.
Returns void
unRegisterFactory
To unregister a class's factories, so that your own can take their place.
Type parameters
- T
Parameters
type: string | Constructable<T>
The type or name of the class whose factories are unregistered.
optionaloptions: { metadata?: {} }
Pass
metadatato unregister one specific factory. Without it, all of the class's factories are unregistered.optionalmetadata: {}
Returns void
staticgetInstance
The Container is a singleton - this is how you reach it.
Returns Container
The Container class is a box to hold all registered factories.
In Octo, we minimize the use of the
newkeyword 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.
Functions/Container