加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

域驱动设计 – DDD:连接对象是实体对象还是值对象?

发布时间:2020-12-14 04:56:44 所属栏目:百科 来源:网络整理
导读:我正在开发一个应用程序,用户可以以类似于Twitter的方式互相关注. 在阅读DDD之后,我了解到我的用户是实体对象 – 我使用他们的唯一ID来引用它们. 当一个用户“跟随”另一个用户(即形成连接)时,该关系存储在多对多表中.其字段包括FollowerID,TargetID和Status
我正在开发一个应用程序,用户可以以类似于Twitter的方式互相关注.

在阅读DDD之后,我了解到我的用户是实体对象 – 我使用他们的唯一ID来引用它们.

当一个用户“跟随”另一个用户(即形成连接)时,该关系存储在多对多表中.其字段包括FollowerID,TargetID和Status.每个Follower / Target组合只能有两个记录(一个是Active,另一个是Inactive),所以我可以根据它们的属性安全地识别对象.

所以,我认为我的Connection对象是Value Objects,而不是Entity Objects,但我不确定.你能帮我解决这个问题吗?

解决方法

你是正确的,实体是唯一的,并带有身份的概念(即只有一个唯一的用户可以存在).连接依赖于其他用户实体.它代表了两个用户之间的某些方面.这方面是指是否存在活动或非活动连接.如果不包含用户连接的数据,则连接没有标识.它甚至可能在数据库中拥有它自己的主键,但从域的角度来看,它没有自己的身份.

因此,我会说Connection是一个值对象.

为了支持我的结论,Microsoft.Net Architecting Applications for the Enterprise,第187页说:

A value object class represents an entity in the domain that mostly
contains data and lives for the data it contains. A value object is
fully identified by a combination of values it contains. An entity
object,on the other hand,has its own life and rich behavior
regardless of the data it contains. Entity objects are usually objects
with a longer lifetime. A value object represents an aspect of an
entity and can live only in relation to an entity.

另请参阅第189页:

One further comment is needed to explain the difference between entities and value objects. You don’t need a repository or a data mapper for a value object. You need a repository only for an entity. The repository (or the mapper) for a given entity will certainly take care of all value objects that depend on a given entity.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读