Combine MapBinder and Assisted Injection?

495 views
Skip to first unread message

KreeK

unread,
May 26, 2010, 7:24:25 PM5/26/10
to google-guice
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!

Kartik Kumar

unread,
May 26, 2010, 7:51:55 PM5/26/10
to google...@googlegroups.com
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);
   }
}




--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


KreeK

unread,
May 26, 2010, 8:38:45 PM5/26/10
to google-guice
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?

> > google-guice...@googlegroups.com<google-guice%2Bunsubscribe@google groups.com>

KreeK

unread,
May 26, 2010, 9:44:20 PM5/26/10
to google-guice
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 <krishnan.1...@gmail.com> wrote:

> > google-guice...@googlegroups.com<google-guice%2Bunsubscribe@google groups.com>

Kartik Kumar

unread,
May 26, 2010, 11:59:32 PM5/26/10
to google...@googlegroups.com
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.


To unsubscribe from this group, send email to google-guice...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages