Module Design

67 views
Skip to first unread message

egolan

unread,
Feb 10, 2012, 9:34:28 AM2/10/12
to google-guice
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

egolan

unread,
Feb 13, 2012, 1:41:14 AM2/13/12
to google-guice
anyone?

Colin Decker

unread,
Feb 13, 2012, 12:23:33 PM2/13/12
to google...@googlegroups.com
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


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


egolan

unread,
Feb 14, 2012, 9:02:55 AM2/14/12
to google-guice
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

Colin Decker

unread,
Feb 14, 2012, 10:28:17 AM2/14/12
to google...@googlegroups.com
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

Thomas Suckow

unread,
Feb 13, 2012, 1:44:06 AM2/13/12
to google...@googlegroups.com
I am no expert on Guice (only been using it for a few days), so I dare
not comment. However, you posted on Friday, at least give it another
day or two.

--
Thomas Suckow

Reply all
Reply to author
Forward
0 new messages