我试图通过尝试实现一个干净的架构结构来试验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
,但是遵循干净架构的原则,我不想对刚才的实现感到困扰,所以我为它编写了一个接口:
interface IProfilesRepository {
getOne (profileId: string): object
updateOne (profileId: string, profile: object): bool
}
然后我在ProfileDomainEntity
构造函数中注入依赖项,我确保它将遵循预期的接口:
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
,因此,据我所知,令牌是不同的,Nest无法解决依赖。
我发现的唯一解决方案是使用自定义提供程序来手动设置令牌:
@Module({
providers: [
ProfileDomainEntity,
{
provide: 'IProfilesRepository',
useClass: ProfilesRepository
}
]
})
export class ProfilesModule {}
并通过指定要与ProfileDomainEntity
一起使用的标记来修改@Inject
:
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)是不可能的。
而且,如果您使用接口来定义(类型)依赖项,那么您必须使用字符串标记。但是,您也可以使用类本身或其名称作为字符串文字,因此您不需要在注入期间提及它,比如依赖的构造函数。
例:
// *** 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();
}
}
您还可以使用抽象类而不是接口,或者为接口和实现类提供类似的名称(并在就地使用别名)。
是的,与C#/ Java相比,这可能看起来像一个肮脏的黑客。请记住,接口只是设计时间。在我的例子中,AppServiceMock
和AppService
甚至不是继承自接口或抽象/基类(在现实世界中,它们当然应该),只要它们实现方法root(): string
,一切都会起作用。
来自the NestJS docs on this topic的报价:
注意
我们使用ConfigService类而不是自定义标记,因此我们覆盖了默认实现。
你确实可以使用接口,很好的抽象类。一个打字稿功能是推断类的接口(保存在JS世界中),所以像这样的东西将起作用
IFoo.ts
export abstract class IFoo {
public abstract bar: string;
}
Foo.ts
export class Foo
extends IFoo
implement IFoo
{
public bar: string
constructor(init: Partial<IFoo>) {
Object.assign(this, init);
}
}
const appServiceProvider = {
provide: IFoo,
useClass: Foo,
};