Getting started with Cloudfork ActiveItem

ActiveItem is a lightweight persistency framework that uses Amazon SimpleDB for storage in the cloud. It is designed using the ActiveRecord pattern and influenced by the Rails framework. Read our previous post for an introduction and rationale. This post will take you through the steps in creating a small basic example that uses the ActiveItem framework.

Install

First of all, you need to startup your Smalltalk image. Goto the Cloudfork project page to read about installing instructions for your Smalltalk.

Setup
ActiveItem provides an abstraction layer on top of SimpleDB. In order to work with ActiveItem objects, it must have information about your AWS account. This is done by activating the framework using your AWS credentials.

| awsCredentials |
awsCredentials := CFAWSCredentials
		newWith: '<your access key>'
		andSecret: '<your secret access key>'.
CFActiveItem activateWith: awsCredentials.

Class definition
The abstract class CFActiveItem must be the root class of all your ActiveItem objects. At this point, there is no need to specify instance variables.

CFActiveItem subclass: #ShoppingCart
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'MyProject'

Describe the Model
Because SimpleDB does not have the notion of a relational schema for items in its domains, the structure of items must be defined by the class of each storable object. To automatically map a ShoppingCart to a SimpleDB item, ActiveItem needs to know the attributes of a ShoppingCart and its relation to ShoppingCartItem. This description is defined using the class method describe: .

ShoppingCart class>>describe: aShoppingCart
    " self rebuild "

    aShoppingCart
       hasTimestamp: #createdAt ;
       hasString: #customerName ;
       hasMany: #shoppingCartItems.

ShoppingCartItem class>>describe: aShoppingCartItem
    " self rebuild "

    aShoppingCartItem
       hasString: #productName ;
       hasInteger: #quantity ;
       belongsTo: #shoppingCart.

Generate Accessors
Inside the comment of the describe: method you see the expression “self rebuild”. Evaluating this will run the description with an ActiveItemClassBuilder. This helper will add the required instance variables and generate accessors for both the attributes and associations in your ActiveItem class.

Store a new ShoppingCart
Before we can store ShoppingCart objects, we have to create a SimpleDB domain.

ShoppingCart ensureDomainExists.
ShoppingCartItem ensureDomainExists

On default, the domain created will be Cloudfork.ActiveItem.ShoppingCart. You can change this by setting a different DomainShardingStrategy. This will be a topic of a future post. For now, we use this domain.

After creating the ShoppingCart and setting its variables, you simple send save to each object. This message returns with a Boolean indicating a successful operation.

| cart item |
cart := ShoppingCart new.
cart createdAt: TimeStamp now.
cart customerName: 'Dennis'.
cart save.

item := ShoppingCartItem new.
item productName: 'Orange Teapot'.
item quantity: 2.
item shoppingCart: cart.
item save.

Retrieving the ShoppingCart
CFActiveItem provides several class methods to retrieve objects stored in SimpleDB. Simple Smalltalk blocks can be used to specify the selection criteria in finding objects. Try inspecting this code fragment.

| cart |
cart := ShoppingCart findFirst: [ :each |
    each customerName = 'Dennis' ].
cart shoppingCartItems.

This post showed the basic steps in using ActiveItem to store objects in Amazon SimpleDB. Each Class requires an implementation of the describe: method as this is the only way ActiveItem knows how to store and retrieve your objects. Future blog posts will discuss the ActiveItem API in more detail. Topics will include Sharding Strategy, Expressions, PagingList access to query results and Attribute Specifications.

Code examples can be found in the packages Cloudfork-ActiveItem-Examples and in Cloudfork-Tests-ActiveItem.

Explore posts in the same categories: Cloudfork

Tags: , ,

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

2 Comments on “Getting started with Cloudfork ActiveItem”

  1. Squeak Says:

    Why not use Magritte to provide the necessary metainformation?
    It’s often used in combination with Seaside applications already.

    [1] http://www.lukas-renggli.ch/smalltalk/magritte

    • emicklei Says:

      The package Cloudfork-SimpleDB-Magritte is to explore this combination. Its goal is to provide a minimal set of classes such that existing applications using Magritte can make use of AWS SimpleDB to store and retrieve objects. Initially, ActiveItem and this Magritte-driven persistency layer are mutual independent packages.


Comment: