Swiz Autowire / Dependency Injection

Jan 21, 2009

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:

[Autowire( 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]
[Autowire(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.

Comments

Joe Rinehart

Joe Rinehart wrote on 01/21/09 2:27 PM

Hey Russell, Funny that you're replacing MG:F with Swiz - it's exactly what I've done. When people ask about MG:F, I'm pointing them to Swiz. On top of autowiring, Swiz can also function as an II framework through its central event dispatcher, making it (effectively) a lighter weight MG. Coupled with its friendliness with DynamicEvent (this is where Cairngorm zealots cringe), you can get a decently architected app together really quickly and nimbly. I <3 Swiz.
Russell Brown

Russell Brown wrote on 01/21/09 3:28 PM

I liked MG:F and it served it's purpose, but I really really saw a gaping whole in regards to dependency injection and a few other areas that Swiz has really locked down. Plus it's really hard not to like the new Mediate stuff...
Chris Scott

Chris Scott wrote on 01/21/09 9:34 PM

Hey Russ, loooong time no chat! Glad to see you are getting into Swiz, I'll be looking forward to more posts. One thing I have added this past week that you may want to check out is a new interface IInitializingBean. It defines one method initialize() where you can implement whatever setup logic you want. This will be called post dependency injection by Swiz, just like init-method on Spring/ColdSpring, and Spring's InitializingBean interface. It's a bit less code than using an event listener for Swiz.INIT_COMPLETE, and I'm all about less code these days! Oh, it's not in Swiz-0.0.5 though, you need to build from source. -C

Write your comment



(it will not be displayed)



Subscribe to this comment thread