【问题标题】:Repository transition from DDD to CQRS从 DDD 到 CQRS 的存储库转换
【发布时间】:2013-11-04 13:47:12
【问题描述】:

最初我的 DDD 存储库类似于以下示例:

class PersonRepository {
    Person findByID(id)
    List<Entity> findAll()
    List<Entity> findWithRegex(String)
}

Service 内部提供了将 Entity 对象转换为 DTO 对象的 GUI

现在我正在尝试进入 CQRS。看了其他的例子,看来我的repo应该是这样的:

class PersonReadModel {
    Person findByID(id)
    List<DTO> findAll()
    List<Entity> findWithRegex(String)
}

仅使用 DDD,我的存储库仅返回 Entity 和 List 对象。对于 CQRS,由于许多读取仅用于 UI,因此有许多读取操作会返回直接 DTO,因此 PersonReadModel 看起来不像传统的 DDD 存储库。

这个假设正确吗?我应该让 PersonReadModel 只返回 List 并保留 PersonRepository 返回的 Entity 和 List 对象吗? PersonReadModel 是否应该是包含指向根聚合的内部存储库的链接的服务?

我可以将 DTO 与其实体相关联,因为它们都有一个身份字段。但是,我担心显示的 DTO 与我的域模型中存在的实体的 revision 不同。我见过的所有 CQRS 示例都有 DTO 和带有 identity 字段但没有 revision 的实体。

我应该关注修订吗?

我的猜测是,应用层中的 GUI 代码将构建带有 DTO 和 revision 的消息,并且域层将使用它来确保请求的命令是使用最新版本构建的版本。

【问题讨论】:

  • “修订”是什么意思?

标签: domain-driven-design cqrs ddd-repositories


【解决方案1】:

ReadModel 在查询端。您不必以 DDD 方式构建此部分,而是以易于查询的方式构建它。在一个项目中,我什至使用公共字段读取模型,因为它们只是数据持有者。

@Entity
@Table(name="t_order_detail")
public class OrderDetailReadModel {
    @Id
    public String tracking_id;
    public String booking_contact_name;
    //other fields
}

public class OrderDetailReadModelQueryObject {
    OrderDetailReadModel findBy(String id);

    List<OrderDetailReadModel> findByReg(string regex);
}

在命令方面,聚合存储库被简化,大多数时候只需要 findById() 和 store():

class PersonRepository {
    Person findByID(id)
    void store(Person person);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多