Reflection in ActionScript 3
After spending half the day on getting dynamic class invocation handsome i must admit that it will make no sense for me using it till Adobe reviewed it.
For a technical proof of concept i was trying to put together a service abstraction layer which interacts with a REST-API. Since the API provides different data formats and is quite generic the architectural approach was to use a factory for each data type and another factory for the concrete resource serializer and deserialzer. Combined with reflection in AS3 by using ‘flash.utils.getDefinitionByName’ this should be straight forward. So far the idea… but real life turned my glory plan into a lesson.The first thing to consider when using reflection is that each class that should be accessible by using ’flash.utils.getDefinitionByName’ has to be available at runtime otherwise one will get runntime errors. Since the FlashCompiler strips out each class which is not explicit called in the source you find yourself typing helper classes just to have a reference of them. This breaks the open closed principle because each time a new class is needed it must be added to the helper class. To come around this i decided to put them into a SWC and include this in the project. But this only delays the problem. The SWC contains the classes as expected but on compile time of the main app they will be stripped out again. To come around this i advised the FlashCompiler to inclue the whole library in the SWF of the main app. Maybe not the best approach but i had my freedom by having the classes in one central point.
As mentioned there are two points where reflection was thought to be used. The second time the mighty sword was thought to be used was when the concrete serializer and deserializer have to be invoked. The serializer and deserializer themself where stored into single packages. One for each data type. Also each package contained a factory for the serializers and deserializers because the classes where marked as internal to ensure no one can simply grab them out of the box since they are quite tricky. Putting together all the code and doing the first test run revealed the ugly face of the Flex compiler. Runtime errors occurred and no concrete class for resource processing could be found. After trying to use helper classes and further more moving around class definitions the final test was to change class selectors for the resource deserializers and serializers from ‘internal’ to ‘public’. Et voilá : everything went well from this point on.
Of course this is not the wanted solution and i am afraid that i surly will spend some more time to investigate but for now i will leave things as they are and fall over into my bed.
Best,
Frank