Dependency Injection
Every exported data model, job, route, and service in RapidREST is automatically detected by the ClassLoader and instances managed by
ObjectFactory. The ObjectFactory creates class instances, resolves their dependencies, and runs lifecycle hooks. All of this is driven by property and
method decorators from the @rapidrest/core library.
ObjectFactory
Once the ClassLoader has scanned and imported all classes in the project source they are registered with the ObjectFactory by their fully
qualified name. The ObjectFactory manages instances of each class and controls each object's lifecycle, including instantiation and destruction.
Objects can be instantiated by either calling the newInstance function or by reference from another class using the @Inject decorator. In either
case, newInstance or @Inject accepts either the actual type to instantiate or the fully qualified name of the class. During object instantiation,
the ObjectFactory automatically injects class dependencies for each variable that has been decorated with one of the framework's injection
decorators. After instantiation, the ObjectFactory will then call all functions that have been decorated with @Init so that the object may
perform post-construction initialization.
The supported injection decorators are:
@Inject- Provides generic dependency injection for any registered class type@Config- Injects the value of the config key at the specified path (e.g.auth:strategy)@Logger- Injects the application logger utility (e.g. Winston)@Repository- Injects the SQL-compatible datastore repository of the given name@MongoRepository- Injects the Mongo-compatible datastore repository of the given name@RedisConnection- Injects the Redis datastore connection of the given name
A reference to each object that has been constructed by the ObjectFactory is stored in an internal map by a unique instance name. This makes it
easy to support singleton instances for a given class. When using @Inject, the default unique instance is always default unless explicitly
overridden by setting the name option. Note that this means every class with the same injected dependency type will get the same class instance
injected by default.
Instances can be destroyed by calling the destroy function on the ObjectFactory. When an instance is destroyed it is removed from the internal
map and any function marked with @Destroy is called in order to perform any cleanup. Note that calling destroy yourself can cause undefined
behavior if the application is still running.
How it fits together
You rarely construct these classes with new yourself. Instead:
- RapidREST's
ClassLoaderdiscovers every exported class under your source tree (see Auto-Discovery). - Each discovered class is registered with the
ObjectFactory. - When a route, model, or another service is instantiated, the
ObjectFactorywalks its@Inject/@Config/@Loggerproperties, resolves them, and finally runs any@Initmethods.
This means a plain service class with no decorators at all, like a PetService that wraps some business logic, is automatically available for injection anywhere in your project, as long as it's exported from a file somewhere under src/.