InteractiveFrameworks

Fundamentals

The dependency injection container underneath every Nest application: what a provider token really is, factories, async and dynamic modules, scopes, cycles, the module reference, lazy loading, lifecycle hooks and metadata. One lesson per page of the official docs' Fundamentals chapter, on the same cats API.

lessons
10
level
intermediate
time
5 hours
Start with Provider Tokens

Providers in depth

The provider syntax the shorthand hides: tokens, values, factories, async factories, modules configured at import time, and how long an instance lives.

  1. 1
    Provider Tokens

    Separate a provider's token from its recipe: inject a value under a symbol token, bind an abstract class to its implementation, and export a token to another module.

    Read the theory
  2. 2
    Factory and Alias Providers

    Build a provider from configuration with useFactory and inject, and give one instance a second token with useExisting.

    Read the theory
  3. 3
    Asynchronous Providers

    Open a connection and load seed data in async factory providers, so Nest waits for both before the first request is served.

    Read the theory
  4. 4
    Dynamic Modules

    Make a module configurable by its importer with a static register() that returns a DynamicModule, and import a module built by ConfigurableModuleBuilder with options and a global flag.

    Read the theory
  5. 5
    Injection Scopes

    Give a provider one instance per request with the REQUEST token, one per consumer with TRANSIENT and INQUIRER, and see what bubbling does to the classes in between.

    Read the theory

The container at runtime

Working with the container after it is built: cycles, looking providers up by hand, loading modules on demand, lifecycle hooks, and the metadata that guards and interceptors read.

  1. 6
    Circular Dependency

    Resolve two modules and two providers that need each other with forwardRef(), and learn what native ES modules add to the docs' recipe.

    Read the theory
  2. 7
    Module Reference

    Ask the container for providers by hand with ModuleRef: get() by class, resolve() for a transient instance, a global lookup with strict false, and create() for a class the container does not own.

    Read the theory
  3. 8
    Lazy-loading Modules

    Keep a module out of the startup graph and load it with LazyModuleLoader the first time a request needs it, then reuse it from the cache.

    Read the theory
  4. 9
    Lifecycle Events

    Open a connection in onModuleInit and seed data in onApplicationBootstrap, and see from the recorded order why the two hooks exist.

    Read the theory
  5. 10
    Execution Context

    Put metadata on a class as the default and on handlers as the override, mark handlers public with SetMetadata, and read it all in a global guard with getAllAndOverride.

    Read the theory