Spring Framework – Pattern Inspiration

A nice mention from the Spring Actionscript guys regarding my post on the data translator pattern.

6.1.1.7. The IDataTranslator interface

When dealing with web services you will typically receive a response that consists of an XML string (but other formats such as JSON, CSV or anything else are possible too of course). This XML is not particularly useful within an application so naturally this XML will need to be converted into business or value objects. You can choose to perform this conversion either in your delegate or your command class after a response has been received. However, these classes aren’t really meant to perform this task, and that’s why Spring Actionscript offers the IDataTranslator Defines a generic data translator object interface.

The interface, as usual, is a very simple one:

public interface IDataTranslator {
  function translate(data:*):*;
}

This enables an IDataTranslator Defines a generic data translator object implementation to take a generic input and return a generic output.

Now what we’d like to be able to do is inject an IDataTranslator Defines a generic data translator object into a delegate instance when appropriate, since not all delegates will need one naturally. So, what we need is a delegate instance that implements the IDataTranslatorAware Implemented by objects that need a reference to an <code>IDataTranslator</code> instance. interface.

This particular interface will not make you scream in terror of its complexity either:

public interface IDataTranslatorAware {
  function set dataTranslator(value:IDataTranslator):void;
}

Spring Actionscript offers a base class for such a delegate called AbstractDataTranslatorAwareBusinessDelegate Abstract implementation of a business delegate that can be assigned an <code>IDataTranslator</code> instance to do some initial data conversion before the delegate’s result is sent back to it’s initial responder.. If you want to create a delegate that makes use of an IDataTranslator Defines a generic data translator object implementation then simply derive from this class. Here’s a simple example of an AbstractDataTranslatorAwareBusinessDelegate Abstract implementation of a business delegate that can be assigned an <code>IDataTranslator</code> instance to do some initial data conversion before the delegate’s result is sent back to it’s initial responder. subclass:

public class GetProductsDelegate extends
         AbstractDataTranslatorAwareBusinessDelegate {

  public function GetProductsDelegate(service:*=null,
                           responder:IResponder=null,
                           dataTranslator:IDataTranslator=null)
  {
   super(service, responder, dataTranslator);
  }

  public function getProducts():void {
   var token:AsyncToken = service.getProducts();
   addResponderToToken(token);
  }
}

Notice the one line in bold, this is where we call a method in the base class that takes care of intercepting the service call, sending the input to the assigned IDataTranslator Defines a generic data translator object and afterwards sending its result back to the delegate’s responder.

Now, a simplified implementation of an IDataTranslator Defines a generic data translator object that takes an XML response and turns it into an array of objects is shown below:

public class XMLToProductsDataTranslator implements IDataTranslator {

  public function XMLToProductsDataTranslator() {
  }

  public function translate(data:*):* {
   var productsArray:Array = [];
   var productsXML:XML = new XML(data);

   //Conversion logic omitted for clarity...

   return productsArray;
  }
}

To configure the delegate to use this particular IDataTranslator Defines a generic data translator object you can add this bit of markup to your configuration file:

<object id="productTranslator"/>

<object id="productsDelegate">
  <property name="dataTranslator" ref="productTranslator"/>
</object>

Or you can create your own version of the delegate factory that handles this kind of injection.

Note

Credit where credit is due: This addition to the Spring Actionscript Cairngorm extensions was inspired by this blog post by Adam Flater: Adam Flater’s blog

From: Spring Framework: Chapter 6. Spring Actionscript Extensions