简体   繁体   English

NestJS - 如何提供动态模块的服务

[英]NestJS - how to provide services of dynamic modules

I'm struggling to figure out on how to provide services from DynamicModule to regular Modules.我正在努力弄清楚如何提供从 DynamicModule 到常规模块的服务。 Pseudocode below:伪代码如下:

app.module.ts app.module.ts

@Global()
@Module({
  imports: [
    DynamicModule.forRoot(config),
    RegularModule,
  ],
  providers: [],
  exports: [],
})
export class AppModule {}

dynamic.module.ts动态模块.ts

@Module({})
export class DynamicModule implements OnModuleInit, OnModuleDestroy {
  constructor(private dynamicService: dynamicService) {}

  static forRoot(config: Config): DynamicModule {
    return {
      module: DynamicModule,
      imports: [],
      providers: [
        {
          provide: CONFIG_TOKEN,
          useValue: config,
        },
        DynamicService,
      ],
      exports: [
        DynamicService,
      ],
    };
  }
}

dynamic.service.ts动态服务.ts

@Injectable()
export class DynamicService {

  constructor(
    @Inject(CONFIG_TOKEN) private readonly config: Config,
  ) {}
}

regular.module.ts常规模块.ts

@Module({
  imports: [],
  providers: [RegularService, DynamicService],
  exports: [RegularService],
})
export class RegularModule {}

regular.service.ts常规服务.ts

@Injectable()
export class RegularService {

  constructor(
    private readonly dynamicService: DynamicService
  ) {}
}

Providing DynamicService to RegularModule requires to provide CONFIG_TOKEN in RegularModule as well, which seems odd and not practical in case more modules would depend on DynamicService and doesn't seem to be the correct way.向RegularModule 提供DynamicService 还需要在RegularModule 中提供CONFIG_TOKEN,如果更多模块依赖于DynamicService 并且似乎不是正确的方法,这似乎很奇怪且不实用。

What concepts am I missing and what is correct approach to use services of a DynamicModule?我缺少什么概念以及使用 DynamicModule 服务的正确方法是什么?

Would something as forFeature in DynamicModule method would be the right direction? DynamicModule 方法中的 forFeature 会是正确的方向吗?


dynamic modules are modules that need some sort of context defined input, classic example is a database module that would need at least the host url and credentials, which would vary by app.动态模块是需要某种上下文定义输入的模块,经典示例是数据库模块,它至少需要主机 url 和凭据,这会因应用程序而异。
This means that when the forRoot(input) returns, you have a module just like any regular (non dynamic) module.这意味着当forRoot(input)返回时,您将拥有一个与任何常规(非动态)模块一样的模块。 As such, you can make use of the config value inside the dynamic module's service, export the service on the dynamic module, and then inject that service on other modules that import your dynamic module.因此,您可以利用动态模块服务中的config值,在动态模块上导出服务,然后将该服务注入到导入动态模块的其他模块上。
There is no need to also inject the config value on the service that injected the dynamicService .无需在注入dynamicService的服务上也注入config值。

If you need to have direct access to the config value inside of regularService , and that value is being shared across multiple services and modules, then you should take a look at the ConfigModule and treat that config value as env.如果您需要直接访问regularService中的config值,并且该值在多个服务和模块之间共享,那么您应该查看ConfigModule并将该config值视为 env。 If by some very specific reason it cant or should not be in env, then still you should create a separate module for providing this config values.如果由于某些非常具体的原因它不能或不应该在 env 中,那么您仍然应该创建一个单独的模块来提供此config值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM