NestJs - CQRS - 寻找一种将对象作为构造函数参数传递的简单方法(从 DTO 到命令)

问题描述 投票:0回答:0

我想初始化一个命令如下:

const command = new CreateProductCommand(payload);

// In another file
export class Payload {
    public readonly type: string;
    public readonly name?: string;
}

我的命令定义如下:

export class CreateProductTypeACommand {
    public readonly name?: string;
}

我尝试在构造函数中创建一个抽象命令来执行此操作:

export class AbstractCommand<T> implements ICommand {
    constructor(args: T) {
        Object.assign(this, args);
    }
}

// in another file
export class CreateProductTypeACommand extends AbstractCommand<CreateProductTypeACommand> {
    public readonly name?: string;
}

但这不起作用:孩子的构造函数覆盖了父母,并且 name 的值被

undefined
覆盖。此外,该对象最终具有
type
字段。

有什么想法吗?我宁愿不在我的命令类中放置任何样板代码或定义

CreateProductTypeACommandArgs
类型。

javascript node.js typescript nestjs cqrs
© www.soinside.com 2019 - 2024. All rights reserved.