英文:
NestJs - CQRS - Looking for a simple way to pass an object as constructor parameters (From a DTO to a command)
问题
以下是您要翻译的部分:
我想要按照以下方式初始化一个命令:
const command = new CreateProductCommand(payload);
// 在另一个文件中
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);
}
}
// 在另一个文件中
export class CreateProductTypeACommand extends AbstractCommand<CreateProductTypeACommand> {
public readonly name?: string;
}
但这并不起作用:子类的构造函数会覆盖父类的构造函数,`name` 的值被覆盖为 `undefined`。而且对象最终会具有 `type` 字段。
有什么想法吗?我宁愿不在我的命令类中放入任何样板代码或定义一个 `CreateProductTypeACommandArgs` 类型。
英文:
I would like to initialize a Command as Follows:
const command = new CreateProductCommand(payload);
// In another file
export class Payload {
public readonly type: string;
public readonly name?: string;
}
where my Command is defined like this:
export class CreateProductTypeACommand {
public readonly name?: string;
}
I have tried create an abstract command to do this in the constructor:
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;
}
But this dosen't work: the child's constructor overrides the parents and the value for name gets overridden by undefined
. Also the object ends up having the type
field.
Any idea? I would rather not put any boiler plate code in my command classes or define a CreateProductTypeACommandArgs
type.
答案1
得分: 1
I had a similar problem and what I ended up doing was using plainToClass
from the class-transformer package.
For example
import { ClassConstructor, plainToClass } from "class-transformer";
// A neat utility function to check you supply all class properties
function map<T, V extends T>(cls: ClassConstructor<T>, plain: V): T {
return plainToClass(cls, plain);
}
function doStuff() {
const newProfile = {
description: "my-description",
name: "my-name",
recentLocation: { lat: 1, lon: 2 },
userId: "my-user-id",
};
const command = map(CreateProfileCommand, newProfile);
...
}
英文:
I had a similar problem and what I ended up doing was using plainToClass
from the class-transformer package.
For example
import { ClassConstructor, plainToClass } from "class-transformer";
// A neat utility function to check you supply all class properties
function map<T, V extends T>(cls: ClassConstructor<T>, plain: V): T {
return plainToClass(cls, plain);
}
function doStuff() {
const newProfile = {
description: "my-description",
name: "my-name",
recentLocation: { lat: 1, lon: 2 },
userId: "my-user-id",
};
const command = map(CreateProfileCommand, newProfile);
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论