I think my requirement was similar to yours so maybe my solution will 
help you.  I have several different implementations of an interface, 
and I wanted runtime configuration to select which one to use.

interface MyInterface {}

class MyImplA implements MyInterface {}

class MyImplB implements MyInterface {}

class MyModule extends AbstractModule {
  void configure() {
    MapBinder<String, MyInterface> mapBinder = 
MapBinder.newMapBinder(binder(), String.class, MyInterface.class);
    mapBinder.addBinding("A").to(MyImplA.class);
    mapBinder.addBinding("B").to(MyImplB.class);
  }

  @Provides
  MyInterface provideMyInterface(@Named("interface.selection.key") 
String key, Map<String, MyInterface> interfaceMap) {
    return interfaceMap.get(key);
  }
}

Obviously you can use a provider instead of the @Provides method.  

And the key should be bound as a constant (in my case, it comes from a 
properties file that is bound using Names.bindProperies).

You can add the implementation bindings in any module, so long as 
they're exposed to the module that binds the provider.

I think I captured the intention there.  In your case, I would imagine 
that you would bind the IConfiguration object and access that from your 
provider.  Or, if your configuration changes during execution, then you 
may simply want to inject that map into an object that has the 
AggregationStrategyHolder createStrategyHolder(IConfiguration 
configuration, Date toDate) method on it.


Let me know if I can be more clear on any points.

-Jared

On Wed 28 Dec 2011 03:14:38 PM CST, Sam Berlin wrote:
> AssistedInject does something similar.  See: 
> http://code.google.com/p/google-guice/wiki/AssistedInject  & the
> FactoryModuleBuilder javaodc @ 
> http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/assistedinject/FactoryModuleBuilder.html
>  .
>  Rather than changing the response based on the input, though, there's
> a different factory methods that return the different objects.
>  Changing the response based on the input will necessarily require a
> hand-written factory (but that factory can still use Guice-injected
> objects, easing some of the pain).
>
> sam
>
> On Wed, Dec 28, 2011 at 3:47 PM, egolan <[email protected]
> <mailto:[email protected]>> wrote:
>
>     Hi,
>     Our whole project uses hand-written DI and I really want to switch to
>     Guice.
>     Here's something we have and I would like to understand how it's done
>     with Guice.
>
>     we have a factory method, that based on an input string, returns a
>     specific implementation of an Abstract Factory:
>
>     public class TypeFactoryCreator {
>        public static AggregationStrategyHolder
>     createStrategyHolder(IConfiguration configuration, Date toDate) {
>
>     switch (enum based on a string) {
>     ...
>     }
>
>     AggregationStrategyHolder has two members (strategy and abstract
>     factory):
>     public class AggregationStrategyHolder {
>        private final AggregationStrategy aggregationStrategy;
>        private final AggregationFactory aggregationFactory;
>     ...
>     }
>
>     So, the factory method builds the strategy and AggregationFactory
>     based on input string.
>     AggregationFactory is an Abstract Factory design pattern.
>
>
>     So I think that my question should be, how to bind different classes
>     according to runtime input?
>
>     Was I clear ? :)
>
>     Thanks
>
>     Eyal
>
>     --
>     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]
>     <mailto:[email protected]>.
>     To unsubscribe from this group, send email to
>     [email protected]
>     <mailto: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.


-- 
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.

Reply via email to