0
votes

I'm trying to use a service from a services module in the main scaffolded app controller in NestJS.

This is working - helloWorldsService.message displays the expected greeting in the @Get method - but the imports of HelloWorldsService in app.module and app.controller seem redundant, and seems to violate encapsulation of the services by the services module.

Do I have this right, is this how you consume a discreet service from a different module, or am I missing something? The reason I ask is: if this is correct, and you have to directly reference the other classes (e.g. reference HelloWorldService directly in the controller), then I have trouble seeing why one bothers with the providers/imports properties of the @Module declaration.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RouterModule } from './router/router.module';
import { ServicesModule } from './services/services.module'; //<-- import service MODULE
import { EventsModule } from './events/events.module';
import { HelloWorldsService } from './services/hello-worlds/hello-worlds.service'; //<-- import service module SERVICE


@Module({
  imports: [RouterModule, ServicesModule, EventsModule],
  controllers: [AppController],
  providers: [AppService, HelloWorldsService],
})
export class AppModule {}


//Controller code:
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { HelloWorldsService } from './services/hello-worlds/hello-worlds.service'; //<-- importing service again in consuming controller

@Controller()
export class AppController {
  constructor(private readonly appService: AppService, private readonly helloWorldsService: HelloWorldsService ) {}

    @Get()
    getHello(): string {
        return this.helloWorldsService.Message();
    }
}


//services.module
import { Module } from '@nestjs/common';
import { WagerAccountService } from './wager-account/wager-account.service';
import { WagerAccountHttpService } from './wager-account.http/wager-account.http.service';
import { CustomerIdentityHttpService } from './customer-identity.http/customer-identity.http.service';
import { HelloWorldsService } from './hello-worlds/hello-worlds.service';

@Module({
    exports:[HelloWorldsService],
  providers: [CustomerIdentityHttpService, WagerAccountService, WagerAccountHttpService, CustomerIdentityHttpService, HelloWorldsService]
})
export class ServicesModule {}
1

1 Answers

0
votes

If you want to use some services in another service, there are different ways, depends on what's your goal. For example: You have App1Service and App1Module, App2Service and App2Module and want to use App2Service in App1Service

  1. You can use providers
// App1Module.ts
@Module({
 imports: [],
 controllers: [App1Controller],
 providers: [App1Service, App2Service], // only service is imported here
})
export class App1Module {}
  1. Import full Module but don't forget to export the service
// App1Module.ts
@Module({
 imports: [App2Module], // Full Module is imported here
 controllers: [App1Controller],
 providers: [App1Service], // No need to use service import here but need to export App2Module services
})
export class App1Module {}

App2Service MUST be exported in App2Module

// App2Module.ts
@Module({
  imports: [],
  controllers: [App2Controller],
  providers: [App2Service],
  exports: [App2Service] // this service is exported and available in other services
})
export class App2Module {}

If you have some dependency in App2Module to other Modules, e.g App3Module and want to use App3Service in App1Service you should use Option 2. Export App3Service in App3Module and it will be automatically available for App1Service. That's how it works:

  1. You export App3Service in App3Module
  2. You import App3Module in App2Module
  3. You import App2Module in App1Module