Extension point/extension pattern
Extension Point/extension is a very powerful design pattern that promotes loose coupling and offers great extensibility. There are many use cases in LoopBack 4 that fit into design pattern. For example:
BootStrapperthat delegates to
Bootersto handle different types of artifacts
RequestBodyParserthat finds the corresponding
BodyParsersto parse request body encoded in different media types
stopevents of the application life cycles.
To add a feature to the framework and allow it to be extended, we divide the responsibility into two roles:
Extension point: it represents a common functionality that the framework depends on and interacts with, such as, booting the application, parsing http request bodies, and handling life cycle events. Meanwhile, the extension point also defines contracts for its extensions to follow so that it can discover corresponding extensions and delegate control to them without having to hard code such dependencies.
Extensions: they are implementations of specific logic for an extension point, such as, a booter for controllers, a body parser for xml, and a life cycle observer to load some data when the application is started. Extensions must conform to the contracts defined by the extension point.
NOTE: Applications can also benefit from the extension point/extensions pattern by separating common functionality and specific behaviors for the business logic.
Helper decorators and functions
To simplify implementations of extension point and extensions pattern on top of LoopBack 4’s Inversion of Control and Dependency Injection container, the following helper decorators and functions are provided to ensure consistency and convention.
@extensionPoint: decorates a class to be an extension point with an optional custom name
@extensions: injects a getter function to access extensions to the target extension point
extensionFilter: creates a binding filter function to find extensions for the named extension point
extensionFor: creates a binding template function to set the binding to be an extension for the named extension point
addExtension: registers an extension class to the context for the named extension point
The usage of these helper decorators and functions are illustrated in the