'nestjs inject a class or another based on parent class

In a nestjs project I have a ServiceValidationService and a ControllerValidationService, both implement the same interface so they have same methods with same signature. They just do something differently.

All controllers are named Controller and services are named Service.

I want to use the same factory to inject one or the other depending on the type of the parent class that is requesting it.

Is it possible?

I know I could just normally inject one or the other depending on the parent, I am just curious about if it possible to do it dynamically a little bit like:


const configServiceProvider = {
  provide: ConfigService,
  useClass:
    process.env.NODE_ENV === 'development'
      ? DevelopmentConfigService
      : ProductionConfigService,
};

@Module({
  providers: [configServiceProvider],
})
export class AppModule {}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source