Hmm, maybe partial injection is a silly term. How about an option to
ignore missing rules when injecting? Something like strict=false
perhaps.
To put this in context: I have a framework that creates and executes
Commands in response to Events. The factory responsible for this has a
framework-wide injector (shared by all framework actors). After
constructing a Command the factory uses that injector to pump
application-wide values into the Command before calling execute().
But, the Command Class might have declared a dependency on the
concrete Event responsible for it's creation. I could (and currently
do) set up a "temporary" rule on the injector like this:
var eventClass:Class = Class( getDefinitionByName
( getQualifiedClassName( event ) ) );
injector.newRule().whenAskedFor( eventClass ).useValue( event );
injector.injectInto( command );
injector.newRule().whenAskedFor( eventClass ).defaultBehaviour();
command.execute();
I'm not entirely happy with this approach though. This is just one
example, but there are other places where the "temporary" rule might
override previously configured rules. I would prefer to create a
temporary injector just for that Event. Something like:
injector.injectInto( command );
var eventInjector:Injector = SmartyPants.getOrCreateInjectorFor
( this );
eventInjector.newRule().whenAskedFor( eventClass ).useValue( event );
eventInjector.injectInto( command );
eventInjector.newRule().whenAskedFor( eventClass ).defaultBehaviour();
command.execute();
But, this throws an error as the eventInjector doesn't have rules for
any of the Command's other dependencies, and the framework-wide
injector doesn't have a rule for the concrete Event. The strict option
might look like this:
injectInto( targetInstance: Object, strict: Boolean = true );
When set to false, no errors are thrown if a rule doesn't exist for a
dependency.
I'm not too clued up on Guice (I have no Java experience), but perhaps
I am trying to enable what you called sub-injectors.
Many thanks,
Shaun
On Apr 18, 4:41 am, Josh McDonald <
j...@joshmcdonald.info> wrote:
> Yeah, all rules are injector-specific. If your class is annotated
> [Singleton] and you don't specifically create a rule saying otherwise, it'll
> be one singleton per injector.
>
> Not sure exactly what you're trying to achieve with partial injection, but
> you can probably fake it for now using a live injection. the injector will
> attempt to create for a live injection, but if it can't (for example it's
> looking for an interface rather than a class), it will continue silently,
> and like other live injections it'll be populated whenever the injector gets
> a new matching rule, or in the case of a live rule, whenever the value
> changes.
>
> As for more locally scoped contexts, that's related to the robot legs
> problem, and I'm thinking of adding scopes to the rules rather than setting
> up sub-injectors, which is what Guice does. Adding scopes would probably
> mean a sizeable change, but could hve other benefits, and as rules would be
> grouped in hierarchical scopes we could do some verification of the rulesets
> to aid debugging when things don't quite go how you want them to. I'd love
> to hear your thoughts on the matter though, as it sounds like you've been
> giving it some thought as well.
>
> -Josh
>
> 2009/4/17 dtyrell <
Dars...@gmail.com>
>
>
>
>
>
> > Hi Josh, 2 quickies:
>
> > 1. useSingletonOf: I assume this means a scoped Singleton? ie. Only
> > for that Injector. Is this correct? (Or will it be shared between
> > injectors?)
>
> > 2. Partial Injection (for lack of a better term) seems impossible at
> > present. Lets say that I have one main injector for my application/
> > module context, and I create a temporary injector for a more locally
> > scoped context, I am unable to inject into an object that requires
> > both - an error will be thrown if the first injector doesn't have a
> > rule for dependencies that I plan to inject with the second. Is there
> > an option to suppress that error and continue with the injections?
>
> > Many thanks,
> > Shaun
>
> --
> "Therefore, send not to know For whom the bell tolls. It tolls for thee."
>
> Josh 'G-Funk' McDonald
> -
j...@joshmcdonald.info