Thanks, Philippe. The solution you provided is simpler than I thought, although I settled on a slightly different implementation.
I am basically using multibind to add adapters to each of the different Entity types to a factory per Store type. So getting an adapter is always in the context (scoped) by the actual factory (AStoreAdapterFactory, BStoreAdapterFactory, etc). Adapter (isAdapterFor()) AdapterFactory (getAdapter()) AStoreAdapter: Adapter (this is the non-parameterized interface I register with the multibinder) AStoreAdapterFactory: AdapterFactory (has a constructor that takes a Set<AStoreAdapter>) Then I define the specific adapter for each entity. In module 1: XEntityAdapter: AStoreAdapter YEntityAdapter: AStoreAdapter In module 2: XEntityAdapter: BStoreAdapter YEntityAdapter: BStoreAdapter And it works beautifully. thanks, On Tue, Nov 23, 2010 at 4:07 PM, Philippe Beaudoin <[email protected]> wrote: > It looks to me you should write your own factory, containing a: > Map<StoreEntityClassInfo, Provider<Adapter>> > Where StoreEntityClassInfo contains both the store class and the entity class. > > Your factory would then have a: > create(Class<? extends Store> storeClass, Class<? extends Entity> > entityClass); > > The last thing you need to do is register all your Adapter classes > towards your factory. You might be able to do that with a multi > binding. > > Cheers, > > Philippe > > > > On Tue, Nov 23, 2010 at 12:44 PM, Yuri de Wit <[email protected]> wrote: >> I am a bit lost trying to bind generic classes. >> >> Consider the following pseudo classes: >> >> class Store { >> void persist(Entity) >> Adapter getAdapter(Entity) >> } >> class AStore: Store >> class BStore: Store >> >> class Entity >> class XEntity: Entity >> class YEntity: Entity >> >> class Adapter >> class AXAdapter: Adapter >> class AYAdapter: Adapter >> class BXAdapter: Adapter >> class BYAdapter: Adapter >> >> How could I use Guice to implement a factory for adapters? I basically >> want to call Store.persist(Entity) and have the Store base class get >> the right adapter for the specific Store and the specific Entity. So, >> for instance, AStore.getAdapter(XEntity) should return AXAdapter. It >> is as if I could bind <Store,Entity> to a given adapter >> implementation. Of course, it would be ideal if I could have a generic >> implementation of the code instead of having to duplicate the code for >> each Store subclass. >> >> I went as far as using injector.getBindingsByType() but the code >> always returns nothing. Wondering if this is a supported usecase and >> if others had implemented such Many-to-Many bindings before. >> >> thanks in advance. >> >> -- >> 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.
