Injecting into custom namespaces? Yea, Swiz can do that too.
Update: Swiz now supports custom namespaces automatically, so this post is fairly irrelevant. For more info see the Swiz RC1 release notes.
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?
Both comments and pings are currently closed.
