In DDD, what are the actual advantages of value objects?

自闭症网瘾萝莉.ら 提交于 2019-12-03 12:29:58

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.

  • Value objects can be used as arguments for other methods in other classes
  • It can make your design clearer
  • It might help with performance optimization (example: fly-weight pattern)
  • Value objects can be reused in different entities. (example: user and location entities with address value objects.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!