#Understanding Injectable Service Behaviour Across Module Dependencies

12 messages · Page 1 of 1 (latest)

versed anchor
#

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

versed anchor
#

Can someone help me here, I update the value of property user in Service A using two methods

  1. Using API - When i update the value of user, I can get the updated value from service dependent on UserService which is present in Module C but when service from module B dependent on UserService return the old value.
  2. Using NestJs schedular ('@nestjs/schedule') - in this case i can get updated value from both services in module B & C.

Is it something due to service instance is getting updated in injection container in case 2 and case 1 for only module C ?
Why it is not happening for case 1 in module B ?

PS: API & Schedular call the same function to update the value of user.

shut ravine
#

Every time you add a provider to a providers array, you're telling Nest to create that provider in the current module's context. If you only want one instance (usually the case) you just eed to import the module that originally exports the provider

versed anchor
#

Thank You @shut ravine Also can you please help understanding why the instance in 2nd case mentioned above getting destroyed & created with new value but not in case 1 ?

shut ravine
#

I'm not immediately sure from the description alone

versed anchor
#

Here is the example code for the scenario

  • Controller B
import { Controller, Post } from '@nestjs/common';
import { UserService } from './user.service.ts';
@Controller()
export class UserController {
  constructor(private readonly userService: UserService) {}
  @Post('refresh-value')
  async refreshValue(): Promise<IAppResponse> {
    return await this.userService.refreshValue();
  }
}
  • service A
import { Interval } from '@nestjs/schedule';

@Injectable()
export class UserService {
  private user: string = '';

  getUser() {
    return this.user;
  }
  
  @Interval(10000)
  refreshValue () {
     updateUser();
  }
 
  updateUser() {
    const x = ...  // db operation to get the value of x
    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 {}```

When refreshValue function is called via API the new instance is not getting created while function is run because of NestJs schedular then it is getting updated.
shut ravine
#

How are you checking if it's updated?

versed anchor
#

I'm calling the getUser() from other service & checking if it is returning the updated value in db.

#

Not sure what happening with instance but value it is returning is not the latest one

versed anchor
#

@shut ravine am i missing anything here ?

shut ravine
#

I think I would need to see the full context of how these things are intereacting. More than likely, it's not the instance you think it is

versed anchor
#

May be. Do you need any additional information from me ? I am unable to understand why it does not return the latest value when the provider is added to the providers array.