What happens if you just use FooOne or FooTwo as constructor argument types? If you are not getting any compile or run time errors during casting, I am guessing you wont' see any ClassCastException when using specific constructor types instead of generic IFoo.
On Wed, May 26, 2010 at 6:44 PM, KreeK <[email protected]> wrote: > If anyone is interested I posted a quick example on github > > http://github.com/kreek/Commad-Pattern-with-Guice > > On May 26, 4:51 pm, Kartik Kumar <[email protected]> wrote: > > While back, I did something like this, I think I have missed something > but I > > hope I have not. > > > > Instead of binding to Map<String, IFoo>, bind it to Map<String, > IFooFactory> > > where IFooFactory returns an implementation of IFoo based on String > input. > > In your class, that requires IFoo, you need to inject the Map<String, > > IFooFactory> > > > > So you will have > > > > interface IFooFactory { > > IFoo create(String type); > > > > } > > > > In your module, you will have > > > > bind("typeA").toProvider(FactoryProvider.newFactory(IFooFactory.class, > > FooOne.class)); > > > > So in your class, > > > > public class FooOne { > > @AssistedInject > > public FooOne(@Assisted String name) { > > .... > > > > } > > > > } > > > > public class FooMap { > > @Inject > > public FooMap(Map<String, IFooFactory> factories) { > > this.factories = factories; > > } > > > > public void doSomething(String type) { > > Foo foo = factories.get(type).create(type); > > } > > > > > > > > } > > On Wed, May 26, 2010 at 4:24 PM, KreeK <[email protected]> > wrote: > > > Is possible to combine MapBinder and Assisted Injection? > > > > > I have a MapBinder as below in my Module: > > > > > MapBinder<String, IFoo> mapBinder = > > > MapBinder.newMapBinder(binder(), String.class, > > > IFoo.class); > > > > > mapBinder.addBinding(Bar.ONE).to(FooOne.class); > > > mapBinder.addBinding(Bar.TWO).to(FooTwo.class); > > > > > This works great and the IFoo implementations can be retrieved like > > > so: > > > > > private final Map<String, IFoo> foos; > > > > > @Inject > > > public FooMap(Map<String, ICommand> foos) { > > > this.foos = foos; > > > } > > > > > public void doSomething(IBar b) { > > > IFoo foo = foos.get(b.getType()); > > > foo.execute(); > > > } > > > > > I'd like to have the parameter of the 'doSomething' method be > > > @Assisted into the Foo that comes out of the map. I think you can do > > > it with a FactoryProvider for each IFoo implementation. Is it possible > > > to have a single FactoryProvider handle the creation. Could I inject > > > the foos map in a Factory and have them come out with an @Assisted > > > IBar instance in them? > > > > > thanks! > > > > > -- > > > 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]<google-guice%[email protected]> > <google-guice%2bunsubscr...@google groups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/google-guice?hl=en. > > -- > 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]<google-guice%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > -- 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.
