I'm not an expert, but here's what I've done.  I believe I posted an
example with Motorcycles a while back, and it failed then, but the
recent Guice 2.0 pre-release snapshots now support MapBinders of
AssistedInject factories.  I'll update it and post it again.  In the
meantime:

Define a factory:
public interface DialogFactory {
  Dialog create(Window w);
}

In your Module, use a MapBinder<Class<? extends Dialog>,DialogFactory>
to keep the factory Providers:
class MyModule extends AbstractModule {
   void def(MapBinder<Class<? extends Dialog>, DialogFactory> m,
Class<? extends Dialog> dialogClass) {
    m.addBinding(dialogClasss).toProvider( FactoryProvider.newFactory
(DialogFactory.class, dialogClass));
  }
  void configure() {
     MapBinder<Class<? extends Dialog>, DialogFactory> m =
     Mapbinder.newMapBinder(binder(),
                                                    new
TypeLiteral<Class<? extends Dialog>>(){},
                                                    new
TypeLiteral<DialogFactory>(){});

    def(YourDialog.class);
    def(MyDialog.class);
    def(OtherDialog.class);
 }
}

Then use this in your code to create the Dialogs:
public class WindowedDialogFactory {
 @Inject  Map<Class<? extends Dialog>,DialogFactory> dialogFactories;
 public T create(Class<T extends Dialog> dialogClass, Window window) {
   return (T)dialogFactories.get(MyDialog.class).create(window);
  }
}

You might be able to do better on the generics in
WindowedDialogFactory.create.

It seems attractive to automate WindowedDialogFactory using a scope to
determine the current Window; unfortunately; however, in my limited
experience, it's tricky to use scopes during initialization of parent/
child relationships of objects, because usually you want to enter and
exit a scope during the processing of the scoped objects, not during
the creation, so the current Window would be null.

Side note: It's unfortunate that the FactoryProviders are limited to
concrete classes, not interfaces.  In your case, since you're creating
subclasses of Dialog, it's not a limitation, but I've seen cases where
you wind up with a series of Foo and FooImpl objects, and must write
the Factory to return BaseFooImpl instead of just interface Foo.

If you wind up needing annotations, it might make sense to annotate
the Map itself, and have multiple maps.

I've typed this code in from memory so it may not be accurate in
details.  If it's not clear, let me know.
And please trust anything the real Guice folks say over whatever I've
said here.

Leigh.

On Apr 26, 12:28 am, Alexey Kuznetsov <[email protected]> wrote:
> > Take a look at Assisted 
> > Inject.http://code.google.com/p/google-guice/wiki/AssistedInject
>
> Well assisted inject works fine for one class.
> And how I could inject my 20+ dialogs ?  write 20+ factories ? or use
> Named annotation?
>
> Any suggestions?
>
> Alexey.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" 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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to