In DDD, what are the actual advantages of value ob

2019-03-25 08:28发布

问题:

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