ObjectExtender User's Guide and Reference

Using a composer for mapping an attribute to multiple database fields

A composer is used to map a single class attribute to multiple database columns. For example, in the Bank sample, the name for VapCustomer is mapped to three fields in the CUSTOMER table: FIRSTNAME, MIDINIT, and LASTNAME. If a composer does not exist that matches your data, you can create one by creating a new subclass of VapCustomer.

Note that composed attributes used as keys are not supported.

In this example, the VapNameComposer was created.

Do the following:

  1. Create a new class, VapNameComposer, that is a subclass of VapComposer VapAttributeComposer. Next, implement the following methods:
  2. Implement an instance method called targetClass (The target class in this example will be VapName). This should return the name of the class for the instance created as a result of the objectFrom: message sent to the converter. Note that in some cases it may be desirable to create your own target class such as was done in this example.
  3. Implement an instance method called attributeNames . It should return an array of the attribute name strings from the target class. For example:
         ^#('firstName' 'middle' 'lastName')
     
     
    
  4. Implement an instance method called objectFrom: . The argument will be an array containing the values for the attribute string name from the method attributeNames in the same order. This method will set the target class attributes based on these values and return an instance of the target class. For example:
         ^VapName first: (array at: 1) middle: (array at: 2) last: (array at: 3))
     
     
    
  5. Implement an instance method called sourceDatatype . This method should return an array of the data elements' class names passed as a parameter to the objectFrom: message which is sent to the converter.

    For example,

         ^#(String String String)
     
     
    
  6. Implement an instance method called dataFrom: . The argument will be an instance of the target class. This method is responsible for returning a collection of objects that are to go to data store fields. For example:
         ^(Array 
             with: anObject firstName 
             with anObject middle 
             with: anObject last)
     
     
    

The VapName class is used in the Bank sample to map one attribute to several columns. It was used as follows:

  1. Launch the Model Browser.
  2. Select Bank from the Models.
  3. Select VapCustomer from the Model Classes.
  4. Select Edit Class from the Classes menu.

    This opens the Class Editor.

  5. Click New to add a new attribute.

    This opens the Attribute Editor.


    Figure pgs2r not displayed.

  6. Type name in the Name field.
  7. Select VapName from the Type list.

    VapName appears in the Type list because the new subclass of VapComposer, VapNameComposer, was recognized with VapName as its target type. The target type of any VapComposer subclasses you create will also appear here.

  8. Select OK.

This concludes the example.

Changing part of a composed attribute like name does not cause the Customer object to be marked "dirty" and updated in the database. To make the Customer object dirty because of a name change, you must alter the name attribute through the Customer's setName accessor.

Also note that if you have a complex attribute type and you want to change the composer type, you must delete the complex attribute map from the Map Browser and recreate it in the Property Map Editor.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]