typescript - Nestjs 依赖注入(inject)和 DDD/整洁架构

标签 typescript dependency-injection domain-driven-design nestjs clean-architecture

我正在通过尝试实现一个整洁的架构结构来试验 Nestjs,我想验证我的解决方案,因为我不确定我是否理解最好的方法。 请注意,该示例几乎是伪代码,并且缺少或通用了很多类型,因为它们不是讨论的重点。

从我的域逻辑开始,我可能想在如下类中实现它:

@Injectable()
export class ProfileDomainEntity {
  async addAge(profileId: string, age: number): Promise<void> {
    const profile = await this.profilesRepository.getOne(profileId)
    profile.age = age
    await this.profilesRepository.updateOne(profileId, profile)
  }
}

这里我需要访问profileRepository,但是遵循clean architecture的原则,我不想被刚才的实现所困扰,所以我为它写了一个接口(interface):

interface IProfilesRepository {
  getOne (profileId: string): object
  updateOne (profileId: string, profile: object): bool
}

然后我在 ProfileDomainEntity 构造函数中注入(inject)依赖项,并确保它遵循预期的接口(interface):

export class ProfileDomainEntity {
  constructor(
    private readonly profilesRepository: IProfilesRepository
  ){}

  async addAge(profileId: string, age: number): Promise<void> {
    const profile = await this.profilesRepository.getOne(profileId)
    profile.age = age

    await this.profilesRepository.updateOne(profileId, profile)
  }
}

然后我创建一个简单的内存实现来运行代码:

class ProfilesRepository implements IProfileRepository {
  private profiles = {}

  getOne(profileId: string) {
    return Promise.resolve(this.profiles[profileId])
  }

  updateOne(profileId: string, profile: object) {
    this.profiles[profileId] = profile
    return Promise.resolve(true)
  }
}

现在是时候使用模块将所有东西连接在一起了:

@Module({
  providers: [
    ProfileDomainEntity,
    ProfilesRepository
  ]
})
export class ProfilesModule {}

这里的问题是显然 ProfileRepository 实现了 IProfilesRepository 但它不是 IProfilesRepository 因此,据我所知, token 是不同的Nest 无法解决依赖关系。

我找到的唯一解决方案是使用自定义提供程序来手动设置 token :

@Module({
  providers: [
    ProfileDomainEntity,
    {
      provide: 'IProfilesRepository',
      useClass: ProfilesRepository
    }
  ]
})
export class ProfilesModule {}

并通过指定要与 @Inject 一起使用的 token 来修改 ProfileDomainEntity:

export class ProfileDomainEntity {
  constructor(
    @Inject('IProfilesRepository') private readonly profilesRepository: IProfilesRepository
  ){}
}

这是处理我所有依赖项的合理方法,还是我完全偏离了轨道? 有没有更好的解决办法? 我对所有这些东西(NestJs、整洁的架构/DDD 和 Typescript)都是新手,所以我在这里可能完全错了。

谢谢

最佳答案

不可能resolve dependency by the interface in NestJS由于语言限制/功能 (see structural vs nominal typing) .

而且,如果您使用接口(interface)来定义(类型)依赖项,则必须使用字符串标记。但是,您也可以使用类本身,或者它的名称作为字符串文字,这样您就不需要在注入(inject)过程中提及它,比如依赖的构造函数。

例子:

// *** app.module.ts ***
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AppServiceMock } from './app.service.mock';

process.env.NODE_ENV = 'test'; // or 'development'

const appServiceProvider = {
  provide: AppService, // or string token 'AppService'
  useClass: process.env.NODE_ENV === 'test' ? AppServiceMock : AppService,
};

@Module({
  imports: [],
  controllers: [AppController],
  providers: [appServiceProvider],
})
export class AppModule {}

// *** app.controller.ts ***
import { Get, Controller } from '@nestjs/common';
import { AppService } from './app.service';

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

  @Get()
  root(): string {
    return this.appService.root();
  }
}

您还可以使用抽象类而不是接口(interface),或者为接口(interface)和实现类提供相似的名称(并就地使用别名)。

是的,与 C#/Java 相比,这看起来像是一个肮脏的 hack。请记住,接口(interface)只是设计时的。在我的示例中,AppServiceMockAppService 甚至没有继承自接口(interface)或抽象/基类(当然,在现实世界中,它们应该继承),只要他们实现方法 root(): string

引自 the NestJS docs on this topic :

NOTICE

Instead of a custom token, we have used the ConfigService class, and therefore we have overridden the default implementation.

关于typescript - Nestjs 依赖注入(inject)和 DDD/整洁架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52969037/

相关文章:

java - Dagger 单例每次都创建新实例

apache-flex - DDD 和异步存储库

design-patterns - 设计方法 : use case driven vs. 域驱动

domain-driven-design - 命令和事件的命名约定

reactjs - (事件 : MouseEvent<Element, MouseEvent>) => void' 不可分配给类型 '() => Event'

javascript - foreach 没有循环遍历元素?

node.js - fork 一个子进程并注入(inject)依赖

Angular 4 组件继承和 "extending view"

javascript - 如何在返回值之前等待其他事件完成?

c# - 如何将控制反转 (IoC) 与 Azure 辅助角色结合使用