Reducing to a significant difference, identity matters to entities, but does not matter to value objects. For example, someone Name is a value object. A client object may consist of the name of the client (value object), the list <Order> OrderHistory (List of objects), and possibly the default address (usually a value object). The customer has an identifier, and each order will have an identifier, but the name should not; as a rule, within the framework of the object model, the identification of the address probably does not matter.
Value objects can usually be represented as immutable objects; changing one property of an object of value significantly destroys the old object and creates a new one, because you are not as concerned about identity as with content. Correctly, the Equals instance method in Name would return "true" if the properties of the object are identical to the properties of another instance.
However, changing any attribute of an object, such as a Client, does not destroy the client; The client object is usually modified. Identity remains unchanged (at least once the object is saved).
You are probably creating value objects without realizing it; anytime you represent any aspect of Entity by creating a fine-grained class, you have a value object. For example, the IPAddress class, which has some restrictions on valid values ​​but consists of simpler data types, will be a value object. EmailAddress can be a string, or it can be a value object with its own set of behavior.
It is possible that even objects that have an identifier in your database do not have an identifier in your object model. But the simplest case is a combination of some attributes that make sense together. You probably don't want to have Customer.FirstName, Customer.LastName, Customer.MiddleInitial and Customer.Title when you can put them together as Customer.Name; they are likely to represent several fields in your database by the time you think about persistence, but your object model does not care.
JasonTrue Sep 16 '08 at 19:03 2008-09-16 19:03
source share