#dependencyinjection #inject #singleton
I asked this on SO here: https://stackoverflow.com/questions/77783597/in-nestjs-can-i-prevent-multiple-providers-with-the-same-injection-token but my friend suggested this discord too.
Basically I want to ensure that in my app I don't accidentally have both the development and production version of a service provider in my DI-tree. Right now if I write:
@Module({
providers: [
{
provide: 'MyService',
useClass: ProdMyService,
}
],
exports: ['MyService'],
})
export class ProdModule { }
@Module({
providers: [
{
provide: 'MyService',
useClass: DevMyService,
}
],
exports: ['MyService'],
})
export class DevModule { }
@Module({
controllers: [MyController],
imports: [ProdModule, DevModule],
})
export class AppModule { }
class MyController {
constructor(@Inject('MyService') private readonly service: MyServiceInterface) { }
}
it seems like chance which MyService version will get used. I would prefer the app alert me about this as an error.