← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 1971.o.1.c.1.a.1.c.1

命令查询分开承担状态变化

理解等级:中级

业务对象难免要改变状态,可把“改动”与“取值/计算”放在不同操作中:命令保持简单,只负责可观察的状态变化且不返回领域数据;查询和计算不改变可观察状态。这样调用者更容易判断一次调用会不会影响后续行为,复杂规则也能单独测试。这是原书在实体对象上的设计建议,不表示现实系统可以完全不用命令;若一个合适的值对象能承担计算,还可进一步减少修改。参见[[无副作用函数便于安全组合]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 原书第10章|Side -Effect-Free Functions · 查看原书依据

      Obviously, you can't avoid commands in most software systems, but the problem can be mitigated in two ways. First, you can keep the commands and queries strictly segregated in different operations. Ensure that the methods that cause changes do not return domain data and are kept as simple as possible.

    参考来源

    - 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第74章《Side-Effect-Free Functions》(源文件:_epub-src/0321125215_ch10lev1sec2.html;单元01) - 结论依据:原文先承认命令不可避免,再提出把命令和查询严格分离、让修改方法简单且不返回领域信息;后文进一步把该分离的适用对象限定为实体。 - 原始内容:Obviously, you can't avoid commands in most software systems, but the problem can be mitigated in two ways. First, you can keep the commands and queries strictly segregated in different operations. Ensure that the methods that cause changes do not return domain data and are kept as simple as possible.