DDD实体应该按引用还是按ID比较? [英] Should DDD entities compare by reference or by ID?

查看:242
本文介绍了DDD实体应该按引用还是按ID比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始使用DDD时,我在比较实体ID的实体中创建了 Equals()方法。因此,具有相同ID的两个实体对象将被视为相等。

When I started using DDD, I created Equals() methods in my entities that compared the ID of the entity. So two entity objects with the same ID would be considered equal.

在某个时候,我考虑了一下,发现即使处于不同状态的两个实体也不应被视为相等,即使当他们描述相同的事物时(即具有相同的ID)。因此,现在我为实体使用引用相等性。

At some point I thought about that and found that two entities in different states should not be considered equal, even when they describe the same thing (i.e. have the same ID). So now I use reference equality for my entities.

然后我偶然发现了马克·塞曼(Mark Seemann)的答案,他在这里写道

I then stumbled over this answer by Mark Seemann, where he writes


如果实体的ID彼此相等,则实体相等。

Entities are equal if their IDs equal each other.

现在,我当然想知道哪种方法更好。

Now, of course, I'd like to know which approach is better.

编辑:请注意,问题不是,是否同时拥有同一实体的两个实例是一个好主意。我知道在大多数情况下可能不是。

Note that the question is not whether having two instances of the same entity at the same time is a good idea. I'm aware that in most situations it is probably not.

推荐答案

问题是双重的。首先,您真正想知道的是

The question is twofold. First, what you really want to know is


在我编码域时如何处理X语言(或Y框架)强加的术语

How to handle terms that the X language (or Y framework) impose when I code a domain model with it?

例如,C#会强制您定义的任何新概念都继承某些公共方法。 Java包括甚至更多的方法

C# for example imposes you that any new concept you define inherit a certain set of public methods. Java includes even more methods.

我从未听说过领域专家谈论哈希码或实例相等性,但这是(经常被误解)引号不打架的情况之一。来自Evans的建议:只是教开发人员在不属于域接口的情况下不要使用它们。

I've never heard a domain expert talking about hash codes or instance equality, but this is one of those situations when the (often misunderstood) quote "don't fight the framework" from Evans apply: just teach developers to not use them when they do not belong to domain's interfaces.

然后,您想知道的是


什么是实体?

What is an entity? How it relates to its own identity?

从为什么开始!您知道实体是可识别的通用语言术语。

Start with why! You know that entities are terms of the ubiquitous language that are identifiable.

但是为什么

简单:实体描述在上下文时间演变相关的概念>>我们正在解决的问题!

Plain simple: entities describe concepts whose evolution in time is relevant in the context of the problem we are solving!

定义实体的是进化的相关性,而不是相反!身份只是交流工具,可以跟踪演变过程并进行讨论。

It is the relevance of the evolution that defines the entity, not the other way around! The identity is just a communication tool to keep track of the evolution, to talk about it.

例如,考虑一下您:您是一个有名字的人;我们使用您的名字来传达您一生中与世界其他地方的互动情况;仍然,你不是那个名字。

As an example think about you: you are a person with a name; we use your name to communicate about your interactions with the rest of the world during your life; still, you are not that name.

问问自己:为什么我需要比较域实体? 域专家是这样说话吗?还是我只是用DDD的话来描述与关系数据库交互的CRUD应用程序?

对我来说,实际上需要将 Equals(object) GetHashCode()实施到实体中似乎有点基础设施不足的感觉。

Ask yourself: why I need to compare domain entities? Is the domain expert talking this way? Or I'm just using a DDD parlance to describe a CRUD application that interact with a relational database?
To me, the need to actually implement Equals(object) or GetHashCode() into an entity looks like a smell of an inadequate infrastructure.

这篇关于DDD实体应该按引用还是按ID比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆