LESSON 04 / 18·2 min readFoundation

What are custom providers and provider scopes?

Try code in the playground
01 Understand02 Recall03 Prepare

Understand the concept

A provider is a dependency that Nest can supply, such as a class instance, value or factory result. Custom providers control how that dependency is created, and scope controls how long its instances are shared. These choices affect testability, state isolation and allocation cost.

Important ideas

  • useValue: supply a value; useClass: supply a class; useFactory: build a dependency.

  • Singleton is the default; request scope creates an instance per request.

  • Transient scope gives each consumer its own instance; extra instances add overhead.

  • Do not store one user's request data on a singleton service field; concurrent requests can observe shared state.

PAUSE & RECALL

Can you explain it without the notes?

What are custom providers and provider scopes?

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

A provider is a dependency that Nest can supply, such as a class instance, value or factory result. Custom providers control how that dependency is created, and scope controls how long its instances are shared. These choices affect testability, state isolation and allocation cost.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Choose request scope only when lifecycle requirements justify it; singleton is not a license to keep request-specific mutable state.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Dynamic module vs global module?

Adapted from 03_NESTJS.md · “What are custom providers and provider scopes?”. Examples are study fragments; framework behavior can depend on your version.