知识卡片
领域服务不能掏空对象行为
把领域动作放入服务前,先判断它能否自然归属某个实体或值对象(参见[[实体与值对象按身份区分]])。过早抽出会使对象只剩数据,业务行为散落为过程脚本;硬塞给不合适的对象,又会模糊其概念并引入它本不需要的依赖。只有动作本身是领域概念、且没有自然归属时,才用[[领域服务承载独立动作]]表达,并以业务通用语言命名操作,让参数和结果采用领域对象。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 原书第05章|Services · 查看原书依据
But when we force an operation into an object that doesn't fit the object's definition, the object loses its conceptual clarity and becomes hard to understand or refactor.
参考来源
- 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第40章《原书第05章|Services》(源文件:_epub-src/0321125215_ch05lev1sec4.html)
- 结论依据:原文同时警告过早放弃对象行为和强行塞入对象造成的概念与依赖混乱,并要求服务职责及接口属于领域模型。
- 原始内容:Now, the more common mistake is to give up too easily on fitting the behavior into an appropriate object, gradually slipping toward procedural programming. ... But when we force an operation into an object that doesn't fit the object's definition, the object loses its conceptual clarity and becomes hard to understand or refactor. ... Operation names should come from the UBIQUITOUS LANGUAGE or be introduced into it. Parameters and results should be domain objects.