public A(@com.google.inject.name.Named("name") final Map<String, ItemService> itemServices) {
...
}
bind(new TypeLiteral<Map<String, ItemService>>() {}).annotatedWith(com.google.inject.name.Names.named("name")).toProvider(MyMapProvider.class);
@Inject
Map<String, ItemService> itemServices;
in my initial post.
Thanks,
Warren Bell
bind(ItemService.class).annotatedWith(Names.named("datasource1")).to(ItemServiceDataSource1Impl.class);
bind(ItemService.class).annotatedWith(Names.named("datasource2")).to(ItemServiceDataSource2Impl.class);
bind(ItemService.class).annotatedWith(Names.named("datasource3")).to(ItemServiceDataSource3Impl.class);
bind(ItemService.class).annotatedWith(Names.named("datasource4")).to(ItemServiceDataSource4Impl.class);
Each ItemService would represent a different data source. I plan on
exposing each one of these ItemService bindings from a seperate
PrivateModule. Each PrivateModule will have it's own MyBatisModule.
Also the number of ItemService implementations will grow and not all
ItemServices connect to a live database.
So the app may be initially configured like:
datasource1 - live
datasource2 - not live
datasource3 - live
datasource4 - not live
and then the user could reconfigure it to:
datasource1 - not live
datasource2 - not live
datasource3 - live
datasource4 - live
datasource5 - live
I am also wondering if Modules#override(Module... modules) can be used
to switch a data source from "not live" to "live" and back, something
like this:
Modules.override(new LivePrivateModuleWithMyBatisModule()).with(new
NotLivePrivateModuleWithNoMyBatisModule());
where the only exposed binding in each Module would be this in
LivePrivateModuleWithMyBatisModule:
bind(ItemService.class).annotatedWith(Names.named("datasource1")).to(ItemServiceDataSource1Impl.class);
and this in NotLivePrivateModuleWithNoMyBatisModule:
bind(ItemService.class).annotatedWith(Names.named("datasource1")).to(ItemServiceDummyImpl.class);
I am hoping this would replace the first binding with the second binding
effectively turning a live data source to "not live". I know I would
still need to clean up the underlying DataSource some how.
Also, if Injector#createChildInjector(Module... modules) could be used
to add a new Module representing a new DataSource.
Bottom line is that the user will be able to change and add data sources
and switch them from live to "not live", and in my code I want to do
something like this:
@Inject
Map<String, ItemService> itemServices;
...
Item item = itemServices.get(activeDataSource).getItem(itemId);
Please let me know if there is a better way of doing all of this.
Thanks,
Warren Bell