On 21 oct, 21:15, David Chandler <[email protected]> wrote: > Hi Yuan, > > Unfortunately, the mere mention of a need for something does not imply > its current availability :-) I wrote the Activities and Places doc and > really should have left GIN out of it for the time being. The root > issue is that GIN does not have a way to createMeA(Foo.class), as such > a method might impede the GWT compiler's ability to do whole program > optimization as it does today. > > Thus, the only way to implement ActivityMapper.getActivity() or > PlaceHistoryMapper.getPlace() using GIN would be to return an instance > of an Activity or Place that has previously been instantiated by GIN > and injected into to the mapper class. In other words, each Activity > and Place would have to be a singleton, much like Presenter and Place > are in the gwt-presenter framework. But in GWT 2.1, Activity and Place > are designed to be disposable, not singletons, which leaves us with > the need for "if (place instanceof SomePlace) return new > SomePlace()..."
There are Provider<?>s for that use case: @Inject Provider<Foo> fooProvider; ... Foo foo = fooProvider.get(); // returns a new instance, unless the toProvider() has been bound in(Singleton.class). > It seems like it would be possible to create > SomeActivityFactory and SomePlaceFactory classes bound as singletons > in GIN gwt-presenter style, which in turn provide newly-created > instances of SomeActivity and SomePlace, but that requires lots of > boilerplate code... That's what AssistedInject (coming in GIN 1.1, which is waiting for a Guice 2.1 release) aims at solving. http://code.google.com/p/google-guice/wiki/AssistedInject What I think might be missing to GIN is the ability to have assisted injection within the Ginjector: interface ClientFactory extends Ginjector { FooActivity fooActivity(FooPlace fooPlace); } But it might be possible to work around this with a GWT code generator: 1. make an "assisted inject" "base" factory interface: public interface ActivityFactory<A extends Activity, P extends Place> { A create(P place); } 2. create an ActivityMapperWithFactory<?> interface extending ActivityMapper 3. have a code generator look at the factory, providing ActivityFactory<?,?> instances through no-arg methods, in the same way PlaceHistoryMapperWithFactory is generated today and can already be used with a Ginjector as a factory for PlaceTokenizer<?>s >From the user (developer) POV: 1. extend ActivityFactory: public interface FooActivityFactory extends ActivityFactory<Foo, FooPlace> { } 2. create a Ginjector as a factory of ActivityFactory instances: @GinModules(MyGinModule.class) public interface MyGinjector extends Ginjector { FooActivityFactory fooActivityFactory(); BarActivityFactory barActivityFactory(); } 3. extend ActivityMapperWithFactory: public interface MyActivityMapper extends ActivityMapperWithFactory<MyGinjector> { } The code generator invoked by GWT.create(MyActivityMapper.class) (i.e. by simply injecting a MyActivityMapper without any specific binding) would then generate: if (place instanceof Foo) return factory.fooActivityFactory().create((Foo) place); if (place instanceof Bar) return factory.barActivityFactory().create((bar) place)); Steps 1 and 2 could be avoided if GIN allowed assisted injection right at the Ginjector level (see above); which I believe is not currently possible (I'd love to be proved wrong on that) -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" 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-web-toolkit?hl=en.
