在清洁架构中,领域对象和数据传输对象使用接口还是类?Typescript

3
我不太熟悉Typescript,也没有在其中实现架构模式的经验,但我们正在开展一个项目,希望使用Clean Architecture,但我对于使用类(class)还是接口(interface)来声明DTO和Domain对象存有疑虑。
我的假设是对于Domain对象,将其设置为类(class)是个好主意,因为它们可以包含业务逻辑。例如:
export class Person {
  id: string;
  firstName: string;
  lastName: string;
  title: string;

  constructor() {}
  
  someBusinessLogic() {
    // Implement some business logic
  }

但对于DTO,由于它们不应该有任何逻辑,我会使用接口来处理:

export interface PersonEntityDto {
  id: string;
  firstName: string;
  lastName: string;
  title: string;
  updatedAt: string;
  createdAt: string;
}

我是对的吗?

1个回答

1

是的,你的假设是正确的。 但是对于领域对象使用接口并没有被任何“官方”规则否定,有些情况下接口是有用的。 如果你使用接口,我的建议是你应该遵守Liskov原则。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接