微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在开发一个应用程序,用户可以以类似于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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐