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

前端之家收集整理的这篇文章主要介绍了域驱动设计 – 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.

猜你在找的设计模式相关文章