07 / 11

How do you create a multi-provider pattern in NestJS to inject multiple implementations for the same token?

NestJS does not natively support multi-providers like Angular, but the same result is achieved with a factory provider that aggregates all implementations into an array or map and exposes them under a single token.

Multi-provider pattern using a factory
Advantages of this approach:
  1. 1

    Adding a new notifier only requires registering it as a provider and adding it to the factory — no changes to NotificationService.

  2. 2

    Each implementation is a singleton provider and can inject its own dependencies.

  3. 3

    The array token is easy to mock in tests with a simple useValue array.

  4. 4

    Works for any interface-based polymorphism scenario: validators, event handlers, middleware chains.