Beginner question coming from the Java Guice world

2 views
Skip to first unread message

Animesh

unread,
Feb 15, 2009, 10:53:55 AM2/15/09
to Smartypants IOC
Hi

I have only recently started learning flex, so my question may be
stupid because of my lack of understanding of flex/actionscript. Also,
I've only worked on web apps, so I'm also new to writing client side
code. So if my question is related to that lack of knowledge then do
point me in the right direction, and I'll try and come back when I've
understood things better :) With that in the clear, let me proceed..

I'm somewhat confused by

SmartyPants.getOrCreateInjectorFor(this);

Does one need to call this in every mxml component in order to create
a new injector. Can't I instead get an injector from a static factory
and then call

injector.injectInto(this);

The trouble I'm having is that calling
SmartyPants.getOrCreateInjectorFor(this); means we're creating a new
injector for every object/mxml component that gets initialized. As
opposed to in Guice where (usually) we have a single injector
intialized at the start of the application and which is then used
across the application.

Also, is there a way to do this in a way that may reduce the
boilerplate code one will need to write in each mxml? Can the creation
of instances be intercepted in flex, so we may not have to call
injector.injectInto(this);

thanks for the help!
Animesh

Josh McDonald

unread,
Feb 15, 2009, 5:13:14 PM2/15/09
to smartyp...@googlegroups.com
Hi Animesh,

The call:

SmartyPants.getOrCreateInjectorFor(this);

Basically is a static factory, that first checks for an existing injector that would be valid, and if it can't find one, creates it. And we have

injector.injectInto(foo);

As a means of getting injection into objects that were created "behind our back" by the Flex SDK, or by the our application by way of the MXML -> ActionScript translation.

You don't need to do it on every MXML component, in fact depending on what you're trying to do, you don't necessarily need to do it at all. Any objects that are created by the injector will automatically be injected into, but when you're instantiating components via MXML declarations, the creation happens with a bunch of "new" statements and SmartypPants doesn't know about them. We do have an MXML tag for convenience when you want to inject into objects that are instantiated via MXML:

<smartypants:RequestInjection/>

This will simply listen for CreationComplete on the parent component, and make the calls to get the injector and perform the injection. I'm also working at the moment on a slight simplification of the API, and I'm considering a feature that will allow you to schedule this to happen automatically on all newly created components.

Cheers,
-Josh
--
"Therefore, send not to know For whom the bell tolls. It tolls for thee."

Josh 'G-Funk' McDonald
  -  jo...@joshmcdonald.info
  -  http://twitter.com/sophistifunk
  -  http://flex.joshmcdonald.info/

Animesh

unread,
Feb 16, 2009, 12:37:22 AM2/16/09
to Smartypants IOC
My confusion was basically with the fact that
SmartyPants.getOrCreateInjectorFor(this) has a dependency on 'this'
instead of something like SmartyPants.getOrCreateInjector();

The 'this' seems to suggest that the injector is dependent on the
object for which it is being created. I hope you're getting me.

Cheers
Animesh
>   -  j...@joshmcdonald.info

Josh McDonald

unread,
Feb 16, 2009, 1:03:11 AM2/16/09
to smartyp...@googlegroups.com
OK, I get what you mean :)

SmartyPants supports multiple injectors (although as of this morning this is no longer the default behaviour), and as such it keeps track of which injector is tied to which object. It will also traverse up the display hierarchy looking for an injector when in multiple-injector mode, so if you call getOrCreateInjectorFor(someComponent), and there's already an injector created for Application, then you'll get that injector returned instead of creating a new one. There's also a convenience method so you can call:

SmartyPants.injectInto(something)

And it will look up (or create) the injector for you, so you can just embed that line of code in a handler for CreationComplete, or Initialize, or anything else.

-Josh
  -  jo...@joshmcdonald.info
Reply all
Reply to author
Forward
0 new messages