DDD:连接对象是实体对象还是值对象? [英] DDD: Connection objects are Entity Object or Value Object?

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

问题描述

我正在开发一个应用程序,用户可以以类似于Twitter的方式互相关注。

I am working on an application where users can follow each other, in a similar fashion to Twitter.

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

After reading up on DDD, I understand that my users are Entity Objects - I refer to them using their unique ID.

当一个用户跟随另一个用户(即形成一个Connection)时,该关系存储在多对多-很多桌子。其字段包括 FollowerID TargetID Status 。每个追随者/目标组合只有两条记录(一个处于活动状态,另一个处于非活动状态),因此我可以根据其属性安全地标识对象。

When one user 'follows' another (i.e. forms a Connection), the relationship is stored in a many-to-many table. Its fields include FollowerID, TargetID, and Status. There can be only two records for each Follower/Target combination (one Active, the other Inactive), so I can safely identify objects based on their attributes.

所以,我认为我的连接对象是值对象,而不是实体对象,但是我不确定。您可以帮我做出这个决定吗?

So, I think my Connection objects are Value Objects, not Entity Objects, but I'm not sure. Can you help me with this decision?

推荐答案

您是正确的,即实体是唯一的,并且带有标识的概念(即只能存在一个唯一用户)。 连接依赖于其他 User 实体。它代表了两个用户之间的某些方面。这方面是存在活动连接还是不活动连接。在不包含用户正在连接的数据的情况下,连接没有身份。它甚至可能在数据库中有它自己的主键,但是从域的角度来看,它没有它自己的身份。

You are correct that entities are unique and carry the notion of having an identity (i.e. only one unique user can exist). A Connection is dependent on other User entities. It represents some aspect between two users. That aspect is whether there is an active or inactive connection. Without containing the data of which users are connecting, a connection has no identity. It may even have it's own primary key in the database, but from a domain perspective, it has no identity of it's own.

因此,我说连接是一个对象。

Therefore, I would say that Connection is a value object.

To支持我的结论, 用于企业的Microsoft.Net架构应用程序 ,第187页说:

To support my conclusion, Microsoft.Net Architecting Applications for the Enterprise, page 187, says:


值对象类表示域中的实体,大部分为
包含数据,并保留其中的数据。价值对象是
,由它包含的值的组合完全标识。另一方面,实体
对象具有其自身的生命和丰富的行为
,无论其包含的数据如何。实体对象通常是寿命更长的对象
。值对象代表
实体的一个方面,并且只能与实体相关。

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.

这篇关于DDD:连接对象是实体对象还是值对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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