Hi all,
this topic comes up so often that we should really find a solution. :) The
short story is that we might need to fix this issue but could potentially
work without it:
http://code.google.com/p/google-gin/issues/detail?id=95(Allow classes
created by a Generator to participate in dependency
injection)
Long story:
> When you start creating multiple Ginjectors, instances of classes don't
> stay the
same. For example, a Singleton EventBus in two Ginjectors are
> different instance types.
>
If you inject your ginjector somewhere in its own dependency tree, it will
inject itself, i.e. it is automatically bound as a singleton.
> Also, I'm pretty sure you can't put an @Inject on an interface method,
> but I haven't tried.
>
Yes, you can - this is explicitly allowed in Gin for use with Generators.
> Would something like the following improve life for GIN users enough to be
> > worth doing? Or is it just a hack?
> > [...]
>
I think this is perfectly viable - but we're running into the generator
interaction issue here. Right now, Gin cannot use generated code as input
and therefore will not be able to inspect the generated implementation of
MyUiBinder. This could be fine if we were specifying exactly what we want to
be injected into the factory. But as far as I can see, you're injecting the
ginjector and then using it in generated code. How do you know how to
retrieve objects from the Ginjector? In difference to Guice, there is
*no *getInstance(..)
method on a Ginjector.
I think what we want is a class generated by UiBinder that has @Inject
annotated constructor/fields/methods for its required children and is then
used as input by the Gin generator to wire the injection up. But to
accomplish that, we'll need to fix the issue mentioned above.
How does this sound?
Peter
> > public interface UiBinderWithFactory<U, O, F> extends UiBinder<U, O> {
> > /**
> > * Sets a factory to use when instantiating objects that are not
> > * provided via @UiFactory methods or @UiField(provided = true) fields.
> > * <p>
> > * When an instance is needed, a zero args method with an appropriate
> > return type
> > * will be sought on the factory to provide it. If none is found
> > GWT.create()
> > * will be used instead.
> > * <p>
> > * It is a compile time error for the factory to provide ambiguous
> > methods.
> > */
> > setFactory(F factory);
> > }
> >
> > You might wind up with code like…
> >
> > @Inject
> > public MyWidget(MyUiBinder binder) extends Composite {
> >
> > public interface MyUiBinder extends UiBinderWithFactory<Widget,
> MyWidget,
> > MyGinjector> {
> > @Inject setFactory(MyGinjector factory);
> > }
> >
> > …and a few extra getters on your Ginjector.
> >
> > Now injecting an injector is generally a terrible idea, but it's
> something
> > at least. (Does that even work in Gin? And can you put @Inject on an
> > interface method?)
> > What do you think?
> > rjrjr
> >
> > --
> > http://groups.google.com/group/Google-Web-Toolkit-Contributors
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-gin" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-gin%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-gin?hl=en.
>
>
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors