Hi, I'm Ben. This is my blog. I write about Flex,
Flash, software development, and other miscellany.

Injecting into custom namespaces? Yea, Swiz can do that too.

Every now and then someone expresses frustration with the fact that Swiz, and every other Flash Platform dependency injection solution I know of, can only inject into public members. I started thinking about it yesterday and figured it actually wouldn’t be that hard to use a custom namespace rather than public. Granted, it’s not as nice as being able to use private members, but the Flash Player/AVM simply does not allow for that at this time. So tonight I spent about 15 minutes throwing together a proof of concept using Swiz 1.0’s new custom processor feature. What you see here is the result of that 15 minutes.

The first step was to create a custom namespace.

Then I created a view that defined some properties in that namespace.

I then created the custom processor to handle those special [NSInject] tags you see in the view. Subclassing Swiz’s built in InjectProcessor (which handles the standard [Inject] tags) allowed me to override just one simple method to accomplish my goal.

That’s it. It supports both property injection and setter injection, and has a ton of room for improvement. You could easily expand this to support the definition of specific namespaces in the metadata tag, or to read the namespace from the property, or some other enhancement I haven’t thought about. Remember, this was 15 minutes of coding; it has taken me longer to write this post than it did to create the processor and example app.

The ability to extend Swiz with custom processors is revealing itself to be an incredibly powerful feature. I fully believe it will turn out to be far more powerful than we even anticipated when we created it. We’ve not even released the 1.0 final version and people have already created processors related to logging, loading Yahoo Finance data, and mediating AS3 Signals.

What can you build?

Workaround for FB-12316 – Code hinting doesn’t work with DefaultProperty

Last night while working on Swiz I ran into FB-12316 – Code hinting doesn’t work with DefaultProperty. It has been fixed in Flash Builder, but those of us still using Flex Builder were apparently out of luck. What the problem boils down to is that if you are using the [DefaultProperty] metadata tag to point to an Array property, code completion breaks unless the property is decorated with [ArrayElementType] metadata.

My Array was not decorated because it can hold items of any type. Therefore, code completion was borked. I wrestled with the problem for a long time, and then as I was waiting to fall asleep last night it occurred to me that in ActionScript everything is actually a descendant of Object. So this morning the first thing I tried was decorating the property with [ArrayElementType( "Object" )] and it worked!

So there you have it, an easy workaround for Flex Builder users! I hope this helps someone get past this a lot quicker than I did.

Enjoy!