How would you create the map in MyMapProvider.class given the following
bindings:
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
On 1/10/12 7:50 AM, Mathieu Clavel wrote:
> Hello,
>
> You can inject maps via constructors like this :
> - add an annotation to the map parameter in the constructor :
>
> public A(@com.google.inject.name.Named("name") final Map<String,
>> ItemService> itemServices) {
>> ...
>> }
>>
>
> "name" can be changed to any string.
>
> - add a binding in the module class :
>
>> bind(new TypeLiteral<Map<String, ItemService>>()
>> {}).annotatedWith(com.google.inject.name.Names.named("name")).toProvider(MyMapProvider.class);
>>
>
> There I used a provider to get the map, but you can also bind directly to
> an instance.
>
> Hope it helps you,
> Regards,
>
> Mathieu
>
--
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.