Value Objects

In many cases it is a good idea to never expose your persistence layer to the presentation layer directly, this dependency could cause your application to be difficult to maintain. For that reason one can use value objects, these are simple classes that simply encapsulate information to be presented to an end-user. Although these value objects represent different views on the existing business objects, they do need to map onto those objects directly. Most of the time you will want to collect some information coming from several entities. For this reason value objects are coarse-grain objects.

Probably the most common situation where value objects are used is the one where you have a list of items to be displayed, selecting one of them will show that item's details in its entirety. Here we have two value objects: one for the representation in the list, and one for the detailed view.

In the spring cartridge you can simply model your value objects to be classes with the <<ValueObject>> stereotype, you then add attributes for all fields you want to be present. Doing this will generate a simple Java class adhering to the Java Bean specification.

An interesting feature is that you can draw a dependency from any <<Entity>> to a <<ValueObject>> or UML datatype. This will instruct the cartridge to generate a transformation method in the entity's DAO class, this transformation method can be called directly or it can be invoked from any finder or load operation that exists: just use the public constant associated with this transformation and specify it as the optional transformation flag, the next example will clarify this in detail.

howto/org/andromda/test/4/uml.gif

  • Auto-generated source that does not need manual editing
  • Auto-generated source that should be edited manually
  • File that is affected by the modifications applied in this section

Please note that an entity may be dependent on any number of value objects, and different entities may be dependent on the same value object.

Next

As promised we'll learn how to model enumerations, click here.