I discussed this with Bob and I'm getting
closer to formalizing the rules for parent
injectors:

1. Bindings, scopes, interceptors, constants
   and type converters are all inherited.

2. When building JIT bindings, Guice will create
   the binding at the highest-ancestor possible.
   Both parent and children maintain their own
   sets of JIT bindings.

3. No Key may be bound by both an injector
   and one of its ancestors. This includes JIT
   bindings. The lone exception is the key for
   Injector.class. If you inject an Injector, you'll
   get the injector that contains your binding.


New APIs:

I'm going to introduce two new core APIs to make
this all work together.

Injector.createChildInjector(). I prefer this over the
current Guice.createInjector(Injector, Stage, Module)
overload, which newbies are presented with when
calling Guice.createInjector(). It doesn't make
sense to use the same method for both simple
and hierarchical injectors.

Injector.getParent(). This simplifies the contract for
methods like Injector.getBindings(). Otherwise I'd need
to somehow return the union of the bindings for the
injector and its ancestors. It also makes the answer to
Injector.getBindings().get(Key.get(Injector.class)
unambiguous.


Interesting Consequences

One surprising consequence of these rules is that you'll
often get the root injector when you might not otherwise
expect it. For example, this test will fail:

  interface A {
    Injector getInjector();
  }

  class RealA implements A {
    @Inject Injector myInjector;
    public Injector getInjector() {
      return myInjector;
    }
  }

  public void test() {
    Injector parent = Guice.createInjector();
    Injector child = Guice.createInjector(new AbstractModule() {
      @Override protected void configure() {
        bind(A.class).to(RealA.class);
      }
    });

    assertSame(child, child.getInstance(A.class).getInjector());
  }

The reason is that although the binding for A.class is in the
child injector (a linked binding), the binding for RealA.class
(a constructor binding) is in the parent injector.  I do not think
this will be an issue, but it's something we should be mindful
of.

Cheers,
Jesse

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to