NestJS: ;无法向拥有接口的子模块提供实例
我尝试将NestJs模块的控制器模块与域模块解耦(根据干净的体系结构原则),以便依赖关系只流向域。 我不明白如何使它工作,repo接口实现的注入不会发生(嵌套启动时出错) 以下是模块结构:NestJS: ;无法向拥有接口的子模块提供实例,nestjs,clean-architecture,Nestjs,Clean Architecture,我尝试将NestJs模块的控制器模块与域模块解耦(根据干净的体系结构原则),以便依赖关系只流向域。 我不明白如何使它工作,repo接口实现的注入不会发生(嵌套启动时出错) 以下是模块结构: AppModule -> CommunityControllerModule -> DomainModule (provides repoImpl (has service requi
AppModule -> CommunityControllerModule -> DomainModule
(provides repoImpl (has service requiring an unknown
by token) concrete repo interface impl)
CommunityController模块:
import { DomainModule } from 'libs/domain/src';
import { Module } from '@nestjs/common';
import { CommunityController } from './community.controller';
import { CommunityRepositoryImpl } from './community.repository';
@Module({
imports: [DomainModule, CommunityRepositoryImpl],
controllers: [CommunityController],
exports: [CommunityRepositoryImpl],
providers: [
{
provide: 'CommunityRepository',
useClass: CommunityRepositoryImpl,
},
],
})
export class CommunityControllerModule {}
域模块:
import { Module } from '@nestjs/common';
import { CommunityService } from './community.service';
@Module({
providers: [CommunityService],
exports: [CommunityService],
})
export class DomainModule {}
注入失败的社区服务:
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { CommunityRepository } from './community.repo';
@Injectable()
export class CommunityService {
constructor(
@Inject('CommunityRepository') private repo: CommunityRepository,
) {}
}
输出的错误:
> nest start
webpack 5.28.0 compiled successfully in 8218 ms
[Nest] 7204 - 2021-05-05 20:52:05 [NestFactory] Starting Nest application...
[Nest] 7204 - 2021-05-05 20:52:05 [InstanceLoader] CommunityRepositoryImpl dependencies initialized +49ms
[Nest] 7204 - 2021-05-05 20:52:05 [ExceptionHandler] Nest can't resolve dependencies of the CommunityService (?). Please make sure that the argument CommunityRepository at index [0] is available in the DomainModule context.
Potential solutions:
- If CommunityRepository is a provider, is it part of the current DomainModule?
- If CommunityRepository is exported from a separate @Module, is that module imported within DomainModule?
@Module({
imports: [ /* the Module containing CommunityRepository */ ]
})
我错过的是将DomainModule设置为一个 域模块变为:
import { DynamicModule, Module } from '@nestjs/common';
import { CommunityService } from './community.service';
@Module({})
export class DomainModule {
static register(repoImpl): DynamicModule {
return {
module: DomainModule,
imports: [repoImpl],
providers: [
CommunityService,
{
provide: 'CommunityRepository',
useClass: repoImpl,
},
],
exports: [CommunityService],
};
}
}
…CommunityController模块将其与repo实现一起导入:
import { DomainModule } from 'libs/domain/src';
import { Module } from '@nestjs/common';
import { CommunityController } from './community.controller';
import { CommunityRepositoryImpl } from './community.repository';
@Module({
imports: [
DomainModule.register(CommunityRepositoryImpl),
CommunityRepositoryImpl,
],
controllers: [CommunityController],
exports: [CommunityRepositoryImpl],
providers: [
{
provide: 'CommunityRepository',
useClass: CommunityRepositoryImpl,
},
],
})
export class CommunityControllerModule {}