You could do it lots of ways. You could pass a different implementation
class to a module to be bound, you could have a switch in a module based on
some flag, etc. It's just code, so you can do whatever works for you as
long as the interface gets bound to the implementation you want at some
point.

-- 
Colin


On Tue, Feb 14, 2012 at 9:02 AM, egolan <[email protected]> wrote:

> Thanks.
> I prefer to have one general module so in my end2end / integration
> tests I would tests it as well.
>
> Which brings me to another question.
> Suppose I have two implementations for the same interface.
> Before using Guice, I had a factory method, that I used with switch-
> case based on the input in runtime to create the correct
> implementation.
> should I use a similar way, but now, instead of creating
> implementations, I will create different modules?
>
> In other words, how can I use Guice to bind different implementations
> based on some runtime input?
>
> Thanks,
>
> Eyal
>
>
> On Feb 13, 7:23 pm, Colin Decker <[email protected]> wrote:
> > Looks fine to me. Of course, since Guice.createInjector can be passed any
> > number of Modules, you don't need to use one Module that installs the
> > others unless you want to. For instance, rather than passing the
> > MpsServiceModule to the MpsModule constructor and then installing it, you
> > could just pass it to createInjector alongside the MpsModule.  If you
> > wanted to ensure in MpsModule that some binding(s) had been done
> elsewhere,
> > you could use, say, requireBinding(MpsService.class). Up to you.
> >
> > --
> > Colin
> >
> >
> >
> >
> >
> >
> >
> > On Mon, Feb 13, 2012 at 1:41 AM, egolan <[email protected]> wrote:
> > > anyone?
> >
> > > On Feb 10, 4:34 pm, egolan <[email protected]> wrote:
> > > > Hi,
> > > > I would like to consult whether the way I constructed my modules is
> > > > the preferred way.
> >
> > > > I created a module for each part of the application (a Guice Abstract
> > > > module for some logic module in the application).
> > > > I have a main module that in its configure method, it uses
> > > > install(...) method.
> > > > The main class calls this module and the Guice.getInjector() and run
> > > > what it need to run.
> >
> > > > One of the sub module is not created in the main module, but instead
> > > > the main creates it and call the main module with it.
> > > > I did it to allow me to do an end2end test with the main module, but
> > > > with a different sub module.
> >
> > > > Main module:
> > > > public MpsModule(Properties properties, MpsServiceModule
> > > > moduleForMpsService) {
> > > >         this.properties = properties;
> > > >         this.moduleForMpsService = moduleForMpsService;
> > > >     }
> >
> > > >     @Override
> > > >     protected void configure() {
> > > >         Names.bindProperties(binder(), properties);
> > > >         convertToTypes(dateMatcher(), dateConverter());
> > > >         install(new MpsBizModule());
> > > >         install(new MpsLogicModule());
> > > >         install(moduleForMpsService);
> > > >         install(new
> > > > ConfigurationModule(MpsConfigurationName.MpsLastProcessingTime.name
> ()));
> > > >     }
> >
> > > > Here's the sub module that I have different implementation for using
> > > > different service:
> > > > (MpsServiceModule just extends Module)
> > > > public class MpsServiceModuleImpl extends AbstractModule implements
> > > > MpsServiceModule {
> > > >     public MpsServiceModuleImpl() {
> > > >         System.out.println("----------------- MpsServiceModuleImpl
> > > > -----------------");
> > > >     }
> >
> > > >     @Override
> > > >     protected void configure() {
> >
> > > >
> ThrowingProviderBinder.create(binder()).bind(MpsServiceProvider.class,
> > > > IProductMatchService.class).to(
> > > >                 MpsServiceProviderImpl.class);
> > > >     }
> >
> > > > }
> >
> > > > Any comments?
> >
> > > > 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].
> > > 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.
>
>

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