在DDD中,值对象的实际优势是什么?
到目前为止,我知道实体对象有 ID,而值对象没有,但在最常见的示例中,人员实体附加了地址值对象。 创建单独的地址对象而不是仅将地址属性保留在 Person 实体中的最大优点是什么?
I have gotten so far that I understand entity objects have an ID while value object have not, but in the most common example you have the person entity that have a address value object attached to it. What is the big advantage of creating a separate address object instead of just keeping the address properties within the Person Entity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了已经提到的内容之外,Greg Young 非常重视这样一个事实:由于它们是不可变的,因此您可以在创建时验证它们,而不必再担心验证问题。 如果状态无法更改,那么您就知道它始终有效。
In addition to the things already mentioned, Greg Young makes a big deal out of the fact that since they are immutable, you can validate them on creation and never worry about validation again. If the state cannot be changed, then you know it's always valid.
不要忘记“没有 id”并不是值对象和实体之间的唯一区别,不可变也非常重要。
Don't forget that "not having an id" is not the only difference between value objects and entities, being immutable is also very important.
将其视为可重复使用的组件。 您可以将其设置为家庭地址、工作地址,无需太多额外的努力。 您可以使用它来将其他系统与人员实体解耦。 假设您介绍一个商业实体。 它还会有一个地址。
与此主题相关的是另一个重要主题:组合与继承
Think of it as a reusable component. You can make it into a home address, work address without much extra effort. You can use it to decouple other systems from the person entity. Say you introduce a business entity. It will also have an adress.
Related to this subject is another important subject: composition vs. inheritance