Themodule implements anInversion of Control (IoC)container called as a service registry that supportsDependency injection.

The IoC container decouples service providers and consumers. A service providercan be bound to the context with a key, which can be treated as an address ofthe service provider.

The diagram below shows how the Context manages services and their dependencies.

In the example above, there are three services in the Context and each of themare bound to a unique key.

  • controllers.UserController: A controller to implement user management APIs
  • repositories.UserRepository: A repository to provide persistence for userrecords
  • utilities.PasswordHasher: A utility function to hash passwordsPlease also note that depends on an instance ofUserRepository and PasswordHasher. Such dependencies are also managed by theContext to provide composition capability for service instances.

Service consumers can then either locate the provider using the binding key ordeclare a dependency using @inject('binding-key-of-a-service-provider') sothat the service provider can be injected into the consumer class. The codesnippet below shows the usage of @inject for dependency injection.

  • An alternative implementation of the service provider can be bound thecontext to replace the existing one. For example, we can implement differenthashing functions for password encryption. The user management system canthen receive custom password hashing functions.

  • Services can be organized as extension points and extensions. For example,to allow multiple authentication strategies, the authentication componentcan define an extension point as authentication-manager and variousauthentication strategies such as user/password, LDAP, oAuth2 can becontributed to the extension point as extensions. The relation will looklike:

loopback-extension

To allow a list of extensions to be contributed to LoopBack framework andapplications, we introduce Component as the packaging model to bundleextensions. A component is either a npm module or a local folder structure thatcontains one or more extensions. It’s then exported as a class implementing the interface. For example:

  1. ...
  2. import {Component, ProviderMap} from '@loopback/core';
  3. export class UserManagementComponent implements Component {
  4. providers?: ProviderMap;
  5. constructor() {
  6. this.repositories = [UserRepository];
  7. };
  8. }
  9. }

The interaction between the application context and UserManagement componentis illustrated below:

  • Binding providers
  • Decorators
  • Sequence Actions
  • Connectors
  • Utility functions
  • Controllers
  • Repositories
  • Models

Some extensions are meant to extend the programming model and integrationcapability of the LoopBack 4 framework. Good examples of such extensions are:

  • Binding providers
  • Decorators
  • Sequence & Actions
  • Connectors
  • Utility functions
  • Mixins (for application)An application may consist of multiple components for the business logic. Forexample, an online shopping application typically has the following component:

  • UserManagement

  • ShoppingCart
  • AddressBook
  • OrderManagementAn application-level component usually contributes:

  • Controllers

  • Repositories
  • Models
  • Mixins (for models)

You can scaffold a LoopBack 4 extension project using @loopback/cli’slb4 extension command.