Composition relations in Cloudfork-ActiveItem

In UML, the composition relation between objects is a special association that is used to model a “private-container” relationship. The typical class-room example is the Car object having 4 Wheel objects. Although you can replace wheels on a car, one particular Wheel object is never shared with other Car objects.

In Amazon SimpleDB there is no concept of relations ; it is a simple storage of items having attributes (key-value pairs). The Cloudfork-ActiveItem framework can map these relations to foreignkey-like attributes but that should be used with care. Because SimpleDB is not a relational database, operations such as Joins are simply not possible. However, mapping the composition relation fits much better in the SimpleDB storage model. The notion of a SimpleDB item being a container of information is just what it is meant to be.

To illustrate how ActiveItem supports this design construct, I will give an example that models multiple-choice questions for an exam training application. A Question is a composition of 4 Choices ; one of them is the correct answer to that question.

Question class>>describe: aQuestion

  aQuestion
    hasString: #code ;
    hasText: #text ;
    ownsMany: #choices
Choice class>>describe: aChoice

  aChoice
    hasText: #text ;
    hasBoolean: #isAnswer

When saving a Question, ActiveItem will create one SimpleDB item with both the attributes of the Question and the attributes of each Choice:

code -> '010-0001'
text -> 'What language is best for writing Web applications?'
choices.1.text -> 'Java'
choices.1.isAnswer -> 'false'
choices.2.text -> 'Ruby'
choices.2.isAnswer -> 'false'
choices.3.text -> 'PHP'
choices.3.isAnswer -> 'false'
choices.4.text -> 'Smalltalk'
choices.4.isAnswer -> 'true'

By supporting composition, class Choice can be a normal ActiveItem subclass with its own attribute description. However, as you can see in the example, Choice objects do not need an id (actually the collection index is the id).

Because of limitations to the number of attributes per item (currently 256), this composition solution is not suitable for arbitrary large collections. If you expect this for your model then I suggest you use the normal hasMany: method that maps associations using “foreign-key” attributes.

Explore posts in the same categories: Cloudfork

Tags: , , , ,

You can comment below, or link to this permanent URL from your own site.

Comment: