← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 1971.o.1.b.4.d.1.c.4.c.1

查找或创建会模糊对象身份

理解等级:中级

把“查找;找不到就创建”隐藏为一个仓储操作,会让调用方难以区分取得既有实体还是生成新实体,而这个差别在领域中通常重要。若请求的只是值对象,往往可以直接让工厂创建等值实例,无需先全局查找。原书因此建议避免通用 find-or-create;是否能合并流程,须先判断领域身份及对象类型。参见[[仓储找旧对象工厂造新对象]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Domain-Driven Design: Tackling Complexity in the Heart of Software》第46章《原书第06章|Repositories》(源文件:_epub-src/0321125215_ch06lev1sec3.html;末段) - 结论依据:原文明确建议避免 find-or-create,认为实体新旧之别常有领域意义,值对象则可直接经工厂取得新实例。 - 原始内容:This function should be avoided. It is a minor convenience at best. A lot of cases in which it seems useful go away when ENTITIES and VALUE OBJECTS are distinguished. ... Usually, the distinction between a new object and an existing object is important in the domain, and a framework that transparently combines them will actually muddle the situation.