For the past month I’ve had my head buried in the world of Flex programming. It’s an exciting world where rich interfaces can be easily prototyped and built out. Overall, I’m finding that the general enterprise development architecture for the platform is very young. And this can lead to some problems.
Two areas I’ve been focusing on are implementing a test strategy and framework for Flex applications, and implementing an MVC solution for flex applications.
For testing, I’ve been using FlexMonkey, which despite its youth, is a very handy application for testing stand-alone flex applications. It’s not so handy when it comes to testing an integrated application of traditional HTML based web-app which interacts with a Flex-driven application, but that’s an issue for another blog post.
I’ve also been using Mate for MVC implementation. But Mate is more than simply an MVC impl for Flex. It also provides IoC implementation, and a nice tag-based event handling and mapping architecture for interacting with the server.
For the past 24 hours I’ve been wrestling with an issue that emerged when our application was being tested inside of the FlexMonkeyLauncher.swf. We were encountering numerous ActionScript-style NPEs. Objects that were supposed to be bound and populated after a login event, were coming back null. But this was only happening when run inside of the FlexMonkey test runner. When our application was running standalone everything behaved fine.
Each of the bindable variables had one thing in common. They were being populated by Mate property injectors. Think of spring bean injection, but for Flex. When the application was run in FlexMonkey the property injection was silently failing. Eventually, I determined that because our app was not the parent app when FlexMonkey was involved, the Mate binder was not finding the target views in the run-time display list, and property injection was failing silently. It would be nicer if there was a debug log saying something like “Hey, that view you said had that one variable I should use? Remember that one? I can’t find it, so I’m exiting. Have a nice day!”
In the end, the solution was to use the InjectorRegistry singleton provided by Mate to explicitly register any views in my application which contained public variables which needed to be hooked up for bindable property injection. Here’s an example:
<!–
Here’s my Flex UIComponent instance, in this case a VBox.
Under normal circumstances it would have more attributes, but
I’m only calling out the necessary one for the example
–>
<mx:VBox initialize=”onInit()” xmlns:mate=”http://mate.asfusion.com/”><mx:Script>
<![CDATA[
// Import the injector registry
import com.asfusion.mate.ioc.InjectorRegistry;// Define the function to run when the component/view is initialized
private function onInit() : void {
InjectorRegistry.register(this); // Add this VBox to the injector registry
}[Bindable]
public var myBindableArray : ArrayCollection;
]]>
</mx:Script></mx:VBox>
I tried a couple of other approaches, including the <InjectorTarget /> tag, but that didn’t work how I expected it to. I’d definitely be happy to hear from Mate or FlexMonkey folks who have a better workaround for this. But if you’ve run into this incompatibility, hopefully this solution will save you some time.