What is the best way to inject a list with google-guice?

Name is carl picture Name is carl · Jan 7, 2013 · Viewed 12.1k times · Source

I have three classes CircleBuilder, SquareBuilder, and TriangleBuilder implementing the ShapeBuilder interface.

I need to initialize my FormBuilder and notably a BuilderList (extending List<ShapeBuilder>) with one instance of each class with google-guice.

What is the best way?

I know about the provider methods and stuff like this:

@Provides
FormBuilder provideFormBuilder() {
    DatabaseTransactionLog instance = new FormBuilder ( <numerous parameters> );

    ShapeBuilder builder = null ; 
    builder = new CircleBuilder( <numerous parameters> ) ;  
    instance.addBuilder( builder ) ;

    builder = new SquareBuilder( <numerous parameters> ) ;  
    instance.addBuilder( builder ) ;

    // And so on

    return instance;
}

but it would mean that I have to create my FormBuilder manually which defeats the purpose of using guice (because FormBuilder is the top element in my object graph).

I'd love to be able to write something like this:

bind(BuilderList.class).to(CircleBuilder.class);
bind(BuilderList.class).to(TriangleBuilder.class);
bind(BuilderList.class).to(SquareBuilder.class);

Any idea?

Answer

Jeff Bowman picture Jeff Bowman · Jan 7, 2013

Consider Multibindings, which will collect bindings very much like your code snippet. There is no provision for lists through Multibinder, because Multibinder is designed for binding to the same collection in multiple modules, and the element order of a Multibinder list would depend on the order that your Modules were evaluated.

Multibinder<ShapeBuilder> shapeBinder =
    Multibinder.newSetBinder(binder(), ShapeBuilder.class);
shapeBinder.addBinding().to(CircleBuilder.class);
shapeBinder.addBinding().to(TriangleBuilder.class);
shapeBinder.addBinding().to(SquareBuilder.class);

// Now you can inject Set<ShapeBuilder>.

Alternatively, your @Provides method can take in parameters (e.g. CircleBuilder or Provider<CircleBuilder>) so you can have Guice create everything except the List itself. Not only will Guice bind Providers of all bound types automatically, but it will also inject every parameter in any @Provides method.

@Provides List<ShapeBuilder> provideShapeBuilders(
    CircleBuilder circleBuilder,
    SquareBuilder squareBuilder,
    TriangleBuilder triangleBuilder,
    Provider<TrapezoidBuilder> trapezoidBuilderProvider) {
  return new ArrayList<ShapeBuilder>(
      circleBuilder,
      squareBuilder,
      triangleBuilder,
      trapezoidBuilderProvider.get(),
      trapezoidBuilderProvider.get());
}