Hey Kartik,
Thanks for that, it works great. One more question and again not sure
if this is possible, I'm new to Java and Guice, right now I have:
public class FooOne implements IFoo {
private final BarA _barA;
@Inject
public FooOne(@Assisted IBar bar) {
this._bar = (BarA)bar;
}
@Override
public void doSomething() {
// do something with _barA...
}
}
In the above I cast the @Assisted IBar to the BarA implementation
(there could be many IBar impl: BarA, BarB, BarC), is there a way to
avoid the cast?
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%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].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.