0

I have a common interface like so:

trait Thing {
  ...
}

And implementations like so:

class ThingA extends Thing {
  ...
}

class ThingB extends Thing {
  ...
}

Which one to use is determined at runtime, so a pool is configured to provide the right one:

class Things(a: ThingA, b: ThingB) {
  addThing(a)
  addThing(b)

  def getThing(x: String) = { ... }
}

How do I provide all configured Things to the ThingPool without explicitly adding it to the constructor?

As in, I'd like to do the following:

class Things(pool: ThingPool) {
  addThingsFromPool(pool)

  def getThing(x: String) = { ... }
}

... where ThingPool has all Things without explicitly asking for them.

... or, ThingPool could be a List[Thing].

The current state of things requires two additional edits to add a Thing. I'd like to get to the point where all I need to do is add the binding statement in my Guice module.

I have omitted the annotations for clarity.

Scoobie
  • 1,097
  • 2
  • 12
  • 22
  • 1
    I guess it's at odds with your wish, but have you looked into using the Cake Pattern instead of an extra-linguistic DI tool such as Guice? – Randall Schulz Feb 08 '13 at 17:16

1 Answers1

1

It seems you are looking for the Multibinding extension: http://code.google.com/p/google-guice/wiki/Multibindings

val thingBinder = Multibinder.newSetBinder(binder(), classOf[Thing])
thingBinder.addBinding().to(classOf[ThingA])
thingBinder.addBinding().to(classOf[ThingA])

And then to use it

class Things(pool:Set[Thing])
EECOLOR
  • 11,184
  • 3
  • 41
  • 75