In part four of this series, we're going to learning about ValueObjects and the first of a few roles they'll play in our Cairngorm/ColdFusion application. In this context, they they serves as typed representations of our data. Most importantly, because they're not generic instances of Object, they're nicely bindable.
Introduction
In the last part of this series, we learned about the somewhat quirky singleton trick, commonly called getInstance(), that we can use to start making the members of our ModelLocators bindable.
Our goals in this part are:
1. Learn what a ValueObject is 2. Create a ValueObject to represent a Contact 3. Modify our ContactModelLocator to use Contact ValueObjects instead of generic Objects
So, what's a ValueObject?
In OO-ColdFusion parlance, a ValueObject is kind of like the Bean pattern we use to pass data around in ColdFusion. The only major difference is that instead of getters and setters, we can simply create properties. In Flex/AS3, you can define default getters and setters for a property that can then take over if you need to associate logic with a given accessor/mutator.
Example:
In ColdFusion, a ContactBean would probably have the following:
<cfreturn variables.firstname />
</cffunction>
<cffunction name="setFirstname">
<cfargument name="firstname" />
<cfset variables.firstname = arguments.firstname />
</cffunction>
In a Flex 2.0 ValueObject (VO from now on!), we just replace this with the following:
That's a bit like doing "this.firstname" in ColdFusion, but with Flex, we can change it to this without altering the interface of the VO:
public function get firstname():String {
return this.firstname;
}
public function set firstname(firstname:String):void {
this.firstname = firstname;
}
Uses of ValueObjects
First, ValueObjects provide a typed version of your data that you can then use throughout your application. One advantage of typing your data is code hinting: if we go to write a Contact form, and we're working with something like ContactModelLocator.currentContact, typing "ContactModelLocator.currentContact." will allow Flex Builder to present you a list of all of the availabile properties on the ContactVO.
Secondly, and more importantly, with a VO you can mark certain properties Bindable, or the entire Bean's public members Bindable.
A third aspect of VO's that's important is that they serve as a way to send typed information back and forth from ColdFusion, with ColdFusion mapping the ValueObjects to CFCs that you've written. We'll get to that in a later piece of this series.
A Contact VO
Ok, there's not much to explain here. The code is simple. The only thing to really note is that I've placed the [Bindable] metadata directive at the top of the class definition. For those following along at home with all of the parts of this series, create a directory named "vo" under the existing /com/firemoss/cg4cf/model directory in your Flex Builder 2 project.
In this file, paste the following code (and read it under you understand it - shouldn't take long!):
{
import com.adobe.cairngorm.vo.IValueObject;
[Bindable]
public class Contact implements IValueObject
{
public var contactId:Number;
public var firstname:String;
public var lastname:String;
public function Contact() {
this.contactId = 0;
this.firstname = "";
this.lastname = "";
}
}
}
That's that.
Using Contact VO's in our ContactModelLocator
Our next order of business is to modify our ContactModelLocator to use the Contact instead of the generic "Object" to represent our Contacts.
First, we need to import the new class, adding this import statement along side the other import statements:
Second, we'll change our little initialize() function to create contacts that are of type Contact instead of Object, changing lines like:
to
Finishing Up
Ok, our datagrid and text box are now going to be using the typed, bindable Contact ValueObject. If we compile and run our application, type a bit in the text box, and click the button, we'll instantly get the update in the DataGrid.
Next Time
Now that we've figured out our binding woes, we'll move on to the next bit of our application: using Cairngorm features like Commands and Events to create a detail form that lets us add, edit, and delete a given contact. That'll pretty much wrap up the presentation tier of our application, at which point we're going to start talking about how we'll interact with the ColdFusion bits (which I'll just provide as a .zip) via things like Business Delegates.
Comment 1 written by Bosch Home Appliances-Dishwashers-Washers-Dryers on 22 January 2008, at 1:53 AM