We have been having several discussions at MediaRAIN recently about how we should handle data adaptation from the back-end to the Flex front-end.  In most cases, our back-end devs are using CakePHP which bases the object model off of the database schema.  In many of our projects, we found the Flex front-end guys going back and forth with the back-end guys about who should bear the brunt of adapting data stored in the database to the Flex front-end model.

The biggest drawbacks to this approach are that CakePHP does not intrinsically allow the develop to take fields from the database and convert them to a Flex front-end model.  In order to force CakePHP to comply, there is an extra layer that has to be forced into the framework in an unnatural way.  Normally, this hasn’t been a big deal when working with back-end Java solutions as object modeling straight out of the database is a common practice (although Hibernate may have similar problems as CakePHP).  With my background in Java, and my lack of CakePHP understanding, I find it difficult to understand where the CakePHP devs are coming from. 

There are many other benefits as well as drawbacks to modeling on the back-end in a CakePHP environment that have been carefully outlined in Aaron Hardy’s blog post entitled: The Why, What, and Where of Custom AMF Class Adapters.  I have made a few comments as well as some of the other developers at MediaRAIN.  Here are some of the key points from this awesome article:

Pros

  • lack of high coupling
  • less of a chance of refactoring due to database schema changes
  • Decreases SWF size due to an absense of Adapter classes on the front-end
  • Reduces the need to recompile the SWF due to back-end changes (especially handy in AIR when updates need to be sent out).
  • Utilizes more of the server’s juice than the client’s computer (could also be a con depending on the circumstance, but I feel that in the majority of circumstances you can choose to upgrade your server speed, but you are at the mercy of the client’s computer speed.)
  • Maintains the Flex front-end coding conventions and styles (i.e. first_name in PHP, firstName in Flex)
  • Maintains the Flex standard of server communication: the Data Transfer Object pattern
  • You can take advantage of AMF’s object mapping abilities to automatically cast the correct Data Transfer Object in PHP to it’s Flex equivalent without traversing Object hierarchys or Arrays.

Cons

  • Back-end dev may need to twist the arm of the CakePHP (or Hibernate) framework to get it to include a back-end class adapter.
  • By adapting classes on the front-end, a single back-end API can have the ability to serve multiple front-end clients due to its abstract nature, as opposed to a back-end closely tied to one particular front-end implementation.

In short, my feeling is that Data Transfer Objects should be implemented on the server-side and that any departure from this methodology should be the exception rather than the rule. There is a reason why Adobe supports it and why it is used in Cairngorm, Nimbus, PureMVC, and other Flex frameworks and by countless Flex developers throughout the world. Keeping in mind that every good answer seems to involve the words “It depends”, I am forced to not give a concise simple answer, because it really does depend on your implementation.

I’m sure there are plenty of other pros and cons that I am missing, or if anyone would like to comment on this discussion, please do!