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

南楼画角 提交于 2019-12-04 18:56:41

问题


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?


回答1:


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.




回答2:


  • 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.




回答3:


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



来源:https://stackoverflow.com/questions/598832/in-ddd-what-are-the-actual-advantages-of-value-objects

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