Hello! I'm struggling to understand the usage of an injectable service when services from different modules depend on it. I've noticed that when I include the service in the module array versus including it in the providers array, I get different results. Since the injectable service's scope is default singleton, meaning the same instance should be present across the application, I'm unsure if new instances to be created in this case, resulting in different return values. Please take a look at the example code provided for a more detailed understanding
- service A
@Injectable()
export class UserService {
private user: string = '';
getUser() {
return this.user;
}
updateUser(x: string) {
this.user = x;
}
}
- module A
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
@Module({
providers: [UserService],
exports: [UserService], // Export UserService for injection
})
export class UserModule {}```
* Module B
```import { Module } from '@nestjs/common';
import { UserService } from '../user/user.service';
@Module({
providers: [UserService],
})
export class ModuleB {}
- Module C
import { UserModule } from '../user/user.module';
@Module({
imports: [UserModule],
})
export class ModuleC {}
When services from Module B and Module C are dependent on UserService, why do both scenarios produce different outcomes? Also, is it the correct approach to provide a service from another module in the providers array to avoid circular dependencies if services from both modules are interdependent?
Additionally is there a way to know if services are using different instances. Your assistance in understanding this case would be greatly appreciated