5.3 ValueObject

"When you care only about the attributes of an element of the model, classify it as a VALUE OBJECT. Make it express the meaning of the attributes it conveys and give it related functionality. Treat the VALUE OBJECT as immutable. Don't give it any identity and avoid the design complexities necessary to maintain ENTITIES." [4] [DDD2003]

A value object is used to encapsulate semantically related attributes as fine grained classes.

ValueObject Structure

Figure 5.6. ValueObject Structure


ValueObjects are used entity attributes types to encourage code reuse and to simplify refactoring. A ValueObject starts with the valueobject keyword followed by a name like so:

valueobject Address {
    String streetName
    String streetNumber
    String zip
    String city
}

Value objects can be used as the type of an entity attribute as shown the next example.

entity Customer {
    String firstName
    String lastName
    Date birthDate
    String ssn
    Boolean premiumMember
    Address invoiceAddress
}

Note: ValueObject supports the recommendation from the hibernate best practice chapter given below:

"Write fine-grained classes and map them using <component>: Use an Address class to encapsulate street, suburb, state, postcode. This encourages code reuse and simplifies refactoring."

For each value object element one java class is generated.




[4] Evans, 2003, S. 105.