views:

348

answers:

4

Actionscript 3.0 has decent native XML support so I'm not surprised to see a lot of people loading an external XML and then passing the object around to various modules/sections of code.

However my instinct is to create a class (i.e. with statically named/typed fields), populate it from the XML and pass THAT around instead. The advantage is decoupling the struct from the message/data format, but you do lose the searchability XML provides and if you have a lot of nested structures I can see how it might involve a lot of initial processing time to convert it.

So what's the best practice here? For really small projects/use cases I still just use the XML but when it gets bigger, or I have the foresight I tend to go the class route.

A: 

i tend to create "reader" classes which load the data and store it in its own XML object. I then pass whatever data is needed from the XML to other objects as native types.

Jeff Winkworth
so instead of a struct, just a facade on the xml object?sounds like best of both worlds, i like this solution a lot, especially in languages like as3 with get/set accessors
putgeminmouth
+1  A: 

I would say due to the XML object having no static structure it opens up a lot of possibility for errors to creep into systems and also makes the code a lot less readable and understandable.

So I would say yes a class is the way to go, it's extra effort but your system is a lot more structured because of it.

I have in the past experimented with wrapping classes around an XML objects, but that can make the code inside those classes quite messy looking.

I'm currently experimenting with binding XML objects to objects using a XMLObjectBinder class I wrote (pure actionscript projects not Flex) as I find this approach the cleanest as XML is out of the picture as soon as possible. So my classes are now cleaner because of the lack of XML inside them and the binding is usually a one line call from a factory.

On the searchability side of things I agree that you lose that but I would say that a factory pattern combined with some XML to Object binding will remove a lot of those issues.

Brian Heylin
A: 

I can't speak to ActionScript, but I think the experience I've had in VB6 and C# may be relevant:

XML message formats change. When they do, code that's tightly coupled to the format of the message breaks. If you can keep all of the code that interoperates with the XML in one place, you can contain the damage to your application that changes in the message format wreaks. Building objects off of the XML and then using the objects in your code allows you to keep most of your code isolated from changes in the message format.

There are plenty of applications where this isn't much of a risk - where, for instance, it's OK to couple the logical structure of the code to the structure of the XML because the XML mirrors the logical structure of the application domain. So you have to use some judgement in deciding which approach to take.

Robert Rossney
A: 

Finally, my preferred solution is to populate the classes from the data but keep a reference to the XML somewhere whenever i need the searchability.

putgeminmouth