Swiz Autowire / Dependency Injection
I'm working on several new flex apps and using Swiz [by Chris Scott] as my framwork of choice; which BTW I'm absolutly loving. I'm converting one large app that was originally written using Model-Glue-Flex. For those that are not familiar with the framework, Swiz among many things does dependency injection and to me feels very ColdSpring it it's implimentation. Just like ColdSpring You create a file declaring all your beans, and then Autowiring is as simple as:
[( bean="securityController" )]
public var securityController : SecurityController;
However I ran into some issues last night [motivator for this post] where the Autowiring was not "working". I made the ultra-simplistic mistake of not changing my once private variable, to a public one. While I didn't see it stated anywhere specifically, Autowiring will not work with a private variable [which makes perfect sense after the fact]. The app won't throw any errors, it just won't do anything.
Another mini lesson is that when Swiz is loading the framework the needed autowire parts are not immediatly available. This is because at least to date, injection via constructor arguments (like ColdSpring) is not possible.
Old Code [Model-Glue-Flex]private var Environment : EnvironmentVO;
public function DownloaderDelagate(Environment : EnvironmentVO) {
this.Environment = Environment;
/*
Do Stuff With this.Environment Here to Init Class
*/
}
New Code [With Swiz][(bean="EnvironmentVO")]
public var Environment : EnvironmentVO;
public function DownloaderDelagate() {
Swiz.addEventListener(Swiz.INIT_COMPLETE, onSwizComplete);
}
public function onSwizComplete(event : Event) {
/*
Do Stuff With this.Environment Here to Init Class
*/
}
Notice my Swiz listener on INIT_COMPLETE; this will fire once Swiz has injected all my object goodness. I can then proceed and init my class as needed.
Brian Kotek has written a couple great posts on using Swiz [1 & 2] and there is also some decent documentation on the Swiz Google Code Site if you want to learn more about Swiz.
Joe Rinehart wrote on 01/21/09 2:27 PM