It seems every time I start a new project there is always a period of time before the server side and the client side logic begin working in sync. In this post I will present a little tool I created for our last project. The task of this tool is to convert value objects or transfer object (VOs/TOs) from XML into ActionScript. I found it really useful in getting the project off the ground as it allowed the ui team to start creating the interface and using actual tos before the server side was ready to go. Applying the service locator pattern to the service layer allows one to easily switch from XML service implementation to the real services.
In the rest of post I refer to the objects being parsed as TOs or transfer objects. The distinction between a transfer object and a value object is that the latter might contain not only data but also perform some logic as well. Transfer objects simply facilitate the moving of data between the backend and the frontend. They do not perform any logic at all. Now let's start parsing some TOs.
To get the parser going we need to follow several simple steps. First, create an XML file containing the TOs' mock data. Second, setup the parser with the correct directory structure and files. Last, invoke the parser. Here I just use a simple test program to invoke the parser. In a real project you would want to create a separate XML implementation for your services and call the parser from each service.
Before getting started with the parser we need to create a XML file. For our example I'll use 3 classes a UserTO class, a AddressTO class and a RoleTO class. Here is the users.xml file that is to be parsed.
A few things to note about the syntax of the XML file. Each class is contained in a <Class> tag. The type attribute specifies the type of the object. A tag without the type attribute is considered to be a String. The ref tag specifies that the details of the class are located in another file.
After the XML file we need to create TO classes to instantiate and populate with data. These are UserTO.as, AddressTO and RoleTO.as. Since they contain just getters and setters I have omitted the code but you can download the source code from 9mmedia's svn.
So we have our XML ready and our TO objects ready to get populated with data. The last step remaining is to setup the parser. We do this in the XmlToParserConfig.as file. Here is how it's done:
Make sure that the toPackage property is pointing to the correct folder where the TOs are stored and that xmlDirectory is pointing to the folder with the xml files. Also note that all classes you define have to be included in the XmlToParserConfig.createInstances() method. All files need to be referenced at least once so that they are included in the swf. The XmlToParserConfig is passed to the constructor of the XmlToParser as you can see below.
The last step remaining is to run the parser.
Once the file finishes parsing onParseComplete is executed and the users collection is populated with 2 UserTO objects.
You can find the source code in the 9mmedia public svn repository located here.
Certainly there are other ways to accomplish the task of getting the frontend up and running. I have found that using the XmlToParser is an easy way to get a project off the ground quickly without a functioning backend. The greatest part about it being that the switch from mock to real data can be made at anytime in the service locator. Depending on the particular implementation one could set each service individually to use real or mock data.
0 Responses to “ActionScript 3 value object generator”
Leave a Reply