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
A:
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
Cristian Libardo
2009-02-28 22:58:44
+2
A:
- 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.
Paco
2009-02-28 23:00:56
+4
A:
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.
jlembke
2009-04-18 05:23:39
Finally found a great reason why immutability is worth something. "It prevents strange behaviour" excuse didn`t convince me before.
Arnis L.
2009-05-04 14:39:59