Can anyone help me to understand DI Nest Fundamentals, my question:
"Is it possible to have a service class without @Injectable annotattion, and also this class does not belong to any module?" I saw on internet an example like below:
This class exists in a common folder:
export class NotificationService {
constructor(
@Inject(Logger) private readonly logger: LoggerService,
private readonly appConfigService: AppConfigService,
@Inject(HttpService) private readonly httpService: HttpService
) {}
async sendNotification(msg: string) {
....
}
}
And then it was registered in another module in the the providers array:
import { Module, Logger, forwardRef, HttpModule } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { NotificationService } from '../../commons/notification/notification.service';
@Module({
imports: [
...
],
controllers: [InvoiceController],
providers: [
InvoiceService,
NotificationService,
Logger],
exports: [InvoiceService]
})
export class InvoiceModule { }
Then it was injected in other service's constructor method
@Injectable()
export class InvoiceService {
constructor(
@Inject(Logger) private readonly logger: LoggerService,
private readonly notificationService: NotificationService) { }
...
}
This works fine, but I don't know why. Why the notification service was injected correctly without add @Injectable, and without and import module?