Official NestJS documentation on providers, covering injection patterns, scoping, manual instantiation, and provider definition methods.
Key takeaways
- Provider scopes: Providers have application lifecycle scope by default (instantiated at bootstrap, destroyed at shutdown). Can also be request-scoped for per-request lifetimes.
- Constructor-based injection: The standard and recommended pattern where dependencies are injected through the constructor. Provides clear visibility of required dependencies.
- Property-based injection: Uses
@Inject()decorator at property level. Useful when passing dependencies throughsuper()in inheritance chains, but generally less clear than constructor injection. - Manual provider instantiation: For dynamic retrieval or bootstrapping scenarios, use Module reference API or standalone application patterns.
- Provider definition methods: Providers can be plain values, classes, or synchronous/asynchronous factories.
Entities and concepts
- Provider Scopes - Application lifecycle vs request-scoped providers
- Constructor-based Injection - Standard dependency injection pattern
- Property-based Injection - Alternative injection using
@Inject()decorator - Manual Provider Instantiation - Module reference and standalone patterns
- Module Reference - Dynamic provider retrieval mechanism
- Provider Definition Methods - Values, classes, and factories
Connections to existing knowledge
These concepts extend the FactoryProvider Pattern documented in Using Redis Client in NestJS. The Provider Scopes concept relates to service lifecycle management, which is relevant for understanding how singleton services (like the Redis client in CacheModule) behave across requests.