Appendix B. SmartyPants for Adobe Flex

Contributed by Josh McDonald

SmartyPants-IOC is a dependency injection framework for use in building Adobe Flex and Flash applications inspired by Google Guice for Java. It's based on key concepts:

  • The class+name key

  • Injector requests, usually specified via metadata

  • Injector rules, specified using an ActionScript-based DSL

The class+name key

Whether you're telling SmartyPants-IOC what to provide or sitting around with your hand out asking for a dependency, the key is the same: a combination of a class and a name. You must specify the class, but the name is optional.

Injection annotations

How do you actually request that a field be injected? In most circumstances you need to annotate your fields with ActionScript metadata. If you want to request an instance of IServiceInterface, the syntax is simple:

[Inject]
public var myService:IServiceInterface;

Let's say you want to look up a particular string, rather than just anything. For a Web Services Description Language (WSDL) URL, or something along those lines, use this syntax:

[Inject(name="mainWSDL")]
public var myServiceWSDL:String;

These fields will be injected into your object when it's constructed for you by SmartyPants-IOC or when you call injector.injectInto(myInstance).

SmartyPants-IOC also supports live injections, which when coupled with live rules behave like the Flex SDK's data-binding functionality:

//This will be set whenever the source changes
[Inject(name="userName",live)]
public var currentUsername:String;

Injector rules

The injector rules are akin to Guice's bindings, but we use another term to avoid confusion with the Flex SDK's data-binding mechanism. You tell the injector what you'd like it to do, using the DSL. Here are a couple of examples:

//Simple singleton rules:
injector.newRule().whenAskedFor(String).named("wsdl")
                  .useInstance("http://www.server.com/soap/service.wsdl");
injector.newRule().whenAskedFor(IServiceInterface)
                  .useSingletonOf(MyServiceImpl);
injector.newRule().whenAskedFor(MyConcreteClass).useSingleton();

//"Live" rules act just like <mx:Binding>
injector.newRule().whenAskedFor(String)
                  .named("userId")
                  .useBindableProperty(this, "userId");

But how do I kickstart the whole thing?

Good question! Two ways. First, in an MXML component, this code will inject into your component on the CreationComplete event:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
     xmlns:smartypants="http://smartypants.expantra.net/2008">

    <smartypants:RequestInjection/>

    <!-- ...Regular code and MXML... -->

</mx:Canvas>

And in ActionScript it's even simpler:

SmartyPants.injectInto(this);

Hopefully this gives a little more insight into the style and ideas behind SmartyPants-IOC. Be sure to check it out on the Google Code site!

SmartyPants-IOC is hosted on Google Code: http://code.google.com/p/smartypants-ioc/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.97.216