DDD - 值对象与数据对象 实体对象
我是 DDD 新手,正在努力理解一些概念。 您如何确定在您的域中哪些对象是实体对象,哪些对象是值对象,以及它们到底是如何区别对待的?
I'm new to DDD and trying hard to understand some of the concepts. How do you determine in your domain what objects are Entity objects and which ones are Value objects, and how exactly are they treated differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,域对象基本上代表业务领域的名词,并且具有身份,而值对象对业务没有任何特殊含义(想想 MonetaryAmount),并且没有身份。
As I see it domain objects basically represent nouns of your business domain and do have an identity, whereas value objects do not carry any special meaning to the business (think MonetaryAmount) and do not have an identity.
来自 Jak Charlton 的博客:
考虑汽车工厂系统应用程序中 POO 中的汽车类(无牌照)。 即使两辆车相同(相同的型号、发动机、颜色、重量等),每辆车都是独一无二的,并且可以通过身份来区分 车辆识别号码。
两辆车可以相等,因为其属性
Car1.equals(Car2)
,但不能是同一辆车,因为其 VINCar1 != Car2
。 如果一辆汽车改变了颜色,那么它就不是其他汽车,而是具有其他属性的同一辆车。 这是一个实体。现在考虑具有
name
和RGB
字段的颜色类(对于汽车)。 青色的名称为'Cyan'
,并且R = 0 G = 255 B = 255
。 不需要其他身份字段,因为它的属性就是它的身份。 颜色是一个 VO,并且必须是不可变的,因为更改名称或 RBG(或两者)代表其他颜色...或者在这种情况下,如果名称和 RGB 不匹配,则会出现错误;)Color1.equals(Color) 和
Color1 == Color2
必须始终具有相同的结果。From Jak Charlton's blog:
Think in a Car Class in POO in a car factory system application (no plate). Every car is unique even if the 2 cars are equals (same model, engine, color, weight, etc) and can be differentiated by a identity Vehicle Identification Number.
Two cars can be equals because its attributes
Car1.equals(Car2)
but not the same car because its VINCar1 != Car2
. If a car change its color it is no other car, it is the same car with other attributes. This is a Entity.Now think in Color Class (for the car) with
name
andRGB
fields. Cyan color has'Cyan'
in name andR = 0 G = 255 B = 255
. No other identity field is needed because its atributes are its identity. Color is a VO and must be inmutable becuase changing name or RBG (or both) represent other color... or a bug in this case if name and RGB doesn't match ;)Color1.equals(Color)
andColor1 == Color2
must always have the same result.