WARNING: Turns out that this is probably not as good of an idea as it first appeared to be, I’m adding some details at the bottom…
I’m working on a really cool Flex 3 project at work. One of the things we’ve done architecturally is to wrap up major portions of the server communication and business logic into manager classes (think of these as MVC controllers.) We use the IMXMLObject interface for these managers so that they can be instantiated in our main Application MXML and we pass managers to other managers explicitly rather than having components reaching up to the application to obtain these references. One of the main drivers for passing this stuff in explicitly is that we’d like the managers to be testable (with FlexUnit) without having to setup loads of application state that isn’t well defined.
Unfortunately when instantiating a class via MXML there is no way that I’m aware of to pass constructor parameters to the object. You can of course set a bunch of properties, but unfortunately there also does not appear to be a way to indicate that certain properties are required prior to a component being used (yeah I realize it’s hard to define when the component is being used.)
I’ve been noodling on this in the back of my mind for a while. Last night I did some searching and didn’t come up with anything. Then I had this idea that we could put a set of conditions into the initialized() method of these components that checks if all of the required properties have been set. If they have, all is good, if they have not, we throw an exception. Yes I know this is simple and probably obvious, but since I couldn’t find references to this after searching for some time, I figured I’d post about it.
Unfortunately this doesn’t allow us to catch a missing dependency at compile time, but the code simply won’t work if a component hasn’t had all of its required attributes set… whereas previously the component could attempt to work and fail in weird and hard to quantify ways. The other downside of this is that we have to add an explicit call to initialized() in all of our unit tests that construct these managers (or their surrogates.) But all-in-all, I feel more comfortable that someone won’t accidentally use one of these managers without first providing all of the required input properties and that even if this is done in Unit tests, the code will fail quickly with a message that will allow folks to provide the missing dependency and move on.
UPDATE:
We pass managers to each other like so: <primarymanager secondaryManager=”{secondaryManagerId}”/>. This is the only way I’m aware of passing other instances of MXML objects to each other. As I’m sure you are aware the squiggly-brace syntax is turned into binding in the background. What you may or may not know is that initialized is called before your binding expressions are initialized!
My solution to this is to throw up my hands in disgust and instantiate my manager objects in the constructor for the code-behind for our application; I can use regular constructor parameters now. I really don’t like this because the object instantiation is no-longer visible to the user working with the application MXML; i.e. they need to know about the manager instances.
What we have here is one more catch-22, can’t pass instances to properties without using binding, and binding doesn’t get called prior to initialized() being called on objects that implement IMXMLObject. I sure wish Adobe would just give us a [Requierd] annotation for properties, also wish they’d allow for parameter initialization with object instances without requiring the overhead of Binding. I only set these things once, don’t need to watch for changes, etc.
If anyone figures out an elegant solution for setting up an initialization contract for MXML components, I’d sure appreciate a comment/link.


