Dec 17
Part 2 – Flex Cairngorm/WebORB Issue Tracker Tutorial – Getting Typed Objects from Rails using [RemoteClass]
This is the second post in a series that covers certain features of Issue Tracker sample app that I added to the Flex RoR SDK.
The first post explained how to set up the application.
This post covers how you can get WebORB for Rails to return typed objects to Flex using the [RemoteClass] metadata tag.
One of the many design patterns that comprises the Cairngorm microarchitecture is the Value Object pattern. Value Objects are used to represent the ‘things’ that you are working with in your application. For example, in the Issue Tracker sample app (view, source) there are currently Value Objects for Users, Projects, and Issues. The class definitions for all of these objects can be found in com.rxr.issuetracker.vo package.
Value Objects on the client generally map to server-side objects. In the Issue Tracker sample app all of the Value Objects listed above map to ActiveRecord classes in Rails, which in turn wrap tables in the database. For instance, the IssueVO class in Flex maps to the Issue class in Rails (see C:\rails\rails_issue_tracker2\app\models\issue.rb) which wraps the issues table in the database.
This is the IssueVO class in Flex:
package com.rxr.issuetracker.vo
{
import com.adobe.cairngorm.vo.IValueObject;
[Bindable]
[RemoteClass(alias="com.rxr.issuetracker.vo.Issue")]
public class IssueVO implements IValueObject
{
public var id : int;
public var project_id : String;
public var reportedby : String;
public var assignedto : String;
public var description : String;
public var status : String;
public var priority : String;
}
}
Which maps to:
class Issue < ActiveRecord::Base
end
Which wraps the issues table:

Prior to WebORB 1.0.9 this mapping was only conceptual, in that Rails would only return untyped objects or ObjectProxys that would have to be 'manually' converted to Value Objects on the client side. Since WebORB 1.0.9 it is now possible to directly map ActiveRecord classes to classes on the Flex side using the [RemoteClass] metadata tag in Flex.
Making this happen is pretty straight-forward:
First, you need to add a [RemoteClass(alias="")] metadata tag just above the class definition of the ActionScript class that you want mapped to an ActiveRecord class.
[Bindable]
[RemoteClass(alias="com.rxr.issuetracker.vo.Issue")]
public class IssueVO implements IValueObject
{
Next, go to C:\rails\rails_issue_tracker2\config and open weborb-config.xml. In this file you need to specify the mapping between your client-side class and your server-side class. For example, the following xml maps the IssueVO class on the client to the Issue class on the server. Note that you use the string specified in the alias of the [RemoteClass] metadata tag as the value of the clientClass in the class mapping.
<classMapping>
<clientClass>com.rxr.issuetracker.vo.Issue</clientClass>
<serverClass>Issue</serverClass>
<source>Issue</source>
</classMapping>
That's it. Now when you make a service call to a method that returns Issue objects in Rails, those objects are automatically returned as IssueVOs to Flex.


December 17th, 2006 at 10:51 am
Great tutorial, but what are the advantages of VO’s and mapping them with RoR?
December 17th, 2006 at 2:33 pm
It looks like a lot of this code could be generated in similar manner to rails scaffolding, even a lot of the state machine for Cairngorm. Any plans for this?
December 17th, 2006 at 6:13 pm
Wilkes,
I agree. I think that nearly all of the code in this sample app could be generated.
I know that Alex MacCaw was working on a Rails Cairngorm generator, but I am not sure where he is at with this project. You can read his post about it here: http://www.eribium.org/?p=34
WebORB for .NET includes generators for Cairngorm (http://blog.themidnightcoders.com/2006/11/weborb-for-net-30rc1-is-released.html), and I believe that the Midnight Coders are planning to migrate all of the functionality in the .NET version to the Rails version.
December 17th, 2006 at 9:38 pm
Niels,
Advantages of VOs:
If used typed VOs then you have control over the definition of the objects that you are working with. You can control what interfaces they implement, which properties are [Bindable], what methods they have, etc.
Using the VO design pattern as a convention will make it easier for others to understand your code.
Steven Webster (Cairngorm\’s author) describes some other benefits in his ariticles about Cairngorm. Go to: http://www.adobe.com/devnet/flex/articles/cairngorm_pt2_03.html and scroll down to \’Introducing the Value Object / Data Transfer Object Pattern\’
Advantage of mapping with WebORB:
If you decide that you want to use VOs, then the advantage to using mapping with WebORB is that you do not have to manually convert the untyped objects that your service calls would normally return to your typed VOs.
Disadvantage:
The disadvantage to this approach is that it ties you to a back-end that implements mapping with the [RemoteClass] metadata tag. So if you want to switch back-ends you will either have to choose another that implements this functionality, or you will have to write code to manually do the conversion.
Derek
December 18th, 2006 at 6:42 am
[...] The second tutorial about the Issue Tracker Flex Application is online. This one is about Getting Typed Objects from Rails using [RemoteClass]. I asked him in de in the comments what the advantages are of VO’s and mapping them with Ruby: [...]
December 21st, 2006 at 2:50 pm
Hi Derek,
Thanks for this it’s a great tutorial, and not just because it’s the only one of its kind
I’m confused by the user login, there appears to be no service for it, is it built-in to WebORB?
Also I saw that WebORB now has setCredentials implemented could this example be tweaked to use it?
thanks!
Alastair
December 21st, 2006 at 7:00 pm
[...] I’ve been messing around with the FlexOnRails Cairngorm/WebORB app and wanted to add some more fields to the flex form for projects (I’m building a construction project tracker so I need to add the project’s address). However as I’m a lazy programmer I quickly got tired of typing all the values to be passed in the event dispatcher. Here’s the original code: [...]
December 21st, 2006 at 8:56 pm
Hi Alastair,
The userService is a bit confusing at first. If I weren’t such a lazy SOB I would have commented the code to make it easier to understand.
If you crack open/config/WEB-INF/flex/remoting-config.xml you will see that the userServiceImpl is mapped directly to the User Active Record instance :
<destination id=”userServiceImpl”>
<properties>
<source>User</source>
</properties>
</destination>
So when you call a method on userServiceImpl it is as though you are calling it directly on the User class in Rails.
Please note that I am not advocating this practice, I just threw this in there to show that it is possible.
I plan to cover this feature in a future tutorial.
I do plan to add setCredentials to this app, but it is my understanding that current implementation in WebORB for Rails is only an iterim solution that will soon be replaced by a more robust solution. So once the update comes out I will add it to the sample app.
Best,
Derek
December 21st, 2006 at 10:04 pm
cool thanks, that clears things up for me.
January 9th, 2007 at 7:19 pm
[...] The second post covered how you can get WebORB for Rails to return typed objects to Flex using the [RemoteClass] metadata tag. [...]
January 15th, 2007 at 12:39 am
[...] The second post covered how you can get WebORB for Rails to return typed objects to Flex using the [RemoteClass] metadata tag. [...]
January 27th, 2007 at 2:14 am
[...] that I have been writing about the Issue Tracker sample app (view, source) I discussed how you can Get Typed Objects from Rails using [RemoteClass]. Now, with the latest version of WebORB, it is also possible send typed objects from Flex to [...]
January 30th, 2007 at 12:44 pm
BTW, Ruby on Rails Cairngorm Generator is released by Ilya Devers and it includes generator for WebOrb Service class.
Also it does:
* Take your application name and create typical Cairngorm structure, including modelLocator, frontController, services.mxml and application itself. You can also set the package structure.
* Create (server) delegates.
* Generate Cairngorm Commands, Events and comments.
* Create standalone events and value objects (though author acknowledges that it could make more sense to create VOs based on model classes).
You can donwload it from Google Code: Ruby on Rails Cairngorm Generators set.
Also, Michael Klishin has posted a great article named “Ruby / DSL Saves Your Soul and Makes You a Happy Flex developer†(Google translation from Russian).
September 23rd, 2007 at 1:27 am
Hi Derek,
Nice tutorial – I wonder if you ran into any problems with nested classes? I have a service that happily returns a ProfilVO, a LoginVO and even a ProfileVO.LoginVO (loginVO member of profileVO). But when I return a ProfilVO containing a LoginVO, the LoginVO’s members are all ‘null’. Did you run into this at all?
Thanks,
Ian