Blog to discuss Midnight Coders products features, ideas and trends in development of Rich Internet Applications

Friday, November 03, 2006

Invoke ActiveRecord Models Directly from Flex

I was doing some work for a client this past week and we were discussing how exactly the MVC design pattern should be subdivided when integrating a Flex client with Rails. Of course the "View" is the Flex UI, and the Model is the data layer on the server. However, the "Controller" piece could easily be on either the client or the server. If this layer is pushed to the Flex client one option is using a framework like Carnigorm (sp?) that provides a "FrontController" for directing requests to the server. However, when this layer resides on the server, when building a WebORB on Rails app, this logic typically manifests itself in the form of a Service (dropped into /app/services) that directs "actions" directly to instance methods just like a true Rails controller.

So what is the point of all this? Well... I still prefer keeping the "controller" logic on the server, but for those that want to push as much processing to the client as possible I've added a couple features to WebORB for Rails that will make direct invocations to Rails models more natural. Most notably you can now invoke class methods on a server-side class as well as expect all of the "dynamic" methods that ActiveRecord supports (like find_all_by_x_and_y) to work as expected. So for example, you can now make a call like:

remoteObject.find_all_by_keyword("Ruby")

...and after mapping this remote object to a Book model in the remoting config, you could expect that a list of books to be returned to the client. Pretty sweet.

One other minor feature that was added is the ability for Rails models to be mapped to a static type on the client (via a [RemoteClass] tag) instead of simply stashing these objects into the standard dynamic ObjectProxy type.

Anywho... I still like introducing the control layer on the server via a proper "service".... but for those of you that want to push the V & C to the client you can now invoke the M directly to get at your data. Gotta love it.

7 Comments:

Anonymous Anonymous said...

Wow thats cool. I would think either is definately a viable way. But yes keeping business logic on the server makes sense. But allowing special requests to be processed client side is very cool, especially around user searching and filtering the UIs diplayed results. Nice feature!

Campbell

2:29 AM

 
Anonymous Mark said...

Harris - this is great stuff and I think lots of folks will find it helpful!

8:24 AM

 
Anonymous Derek Wischusen said...

Harris - Wow! Those are really, really great features. The 'minor' feature that you mention will make it much easier to cleanly implement the ValueObject pattern in Cairngorm.

1:33 PM

 
Anonymous Mike said...

Great features Harris. The direct access to Models is very usefull. An added benefit is that you can now support
the Restfull Web from WebORB.

9:43 AM

 
Anonymous Anonymous said...

It's great to be able to do this. I just don't know what to think of it yet. I'm still a bit attached to service way of doing things.

2:54 PM

 
Blogger maccman said...

Isn't that a security risk, what about authentication? If you link the remote object to a model in the remoting config than surely you have complete control over the model - from the client side! Is this practice only advocated when the security risk isn't an issue?

6:06 PM

 
Anonymous Anonymous said...

For Rails 1.2.1 i needed to add the following route to get the tests to run:

map.weborb '/weborb', :controller => 'weborb', :action => 'index'

In case people get stuck

12:58 AM

 

Post a Comment

<< Home