Context: I have a Store interface for saving various Entities -
Project, ProjectGroup, ProjectNotifier - all extending from
CommonUpdatableEntity:
public interface Store<T extends CommonUpdatableEntity, Q extends
Query<T>> {
public T save(T entity) throws StoreException;
// ...omitted for brevity.
}
and its implementation:
public class JpaStore<T extends CommonUpdatableEntity, Q extends
Query<T>> implements Store<T, Q> {
@Transactional
public T save(T entity) throws StoreException {
// ...omitted for brevity.
return savedEntity;
}
}
configured via a module:
public class StoreModule extends AbstractModule {
protected void configure() {
bind(new TypeLiteral<Store<Project, Query<Project>>>() {
}).to(new TypeLiteral<JpaStore<Project, Query<Project>>>() {
});
bind(new TypeLiteral<Store<ProjectGroup, Query<ProjectGroup>>>() {
}).to(
new TypeLiteral<JpaStore<ProjectGroup, Query<ProjectGroup>>>()
{
});
bind(new TypeLiteral<Store<ProjectNotifier,
Query<ProjectNotifier>>>() {
}).to(
new TypeLiteral<JpaStore<ProjectNotifier,
Query<ProjectNotifier>>>() {
});
}
}
In my web actions (Controllers of MVC), I would like to retrieve the
appropriate Store instance based on the Entity the action is creating/
updating.
To mimic this, I quickly created the following unit test which
attempts to get a Store instance in a similar manner:
public class StoreTest{
@Inject
Injector injector;
//...omitted
@Test
public void testCreateProject() throws StoreException {
Project project = new Project();
project.setName("sample-project");
Store<Project, Query<Project>> store =
getStoreInstance(Project.class);
Assert.assertTrue(null == project.getId()); // FAILS TO GET A
STORE INSTANCE
project = store.save(project);
}
private <T extends CommonUpdatableEntity> Store<T, Query<T>>
getStoreInstance(Class<T> entity) {
List<Binding<Store<T, Query<T>>>> bindings =
injector.findBindingsByType(new TypeLiteral<Store<T, Query<T>>>() {
});
if (bindings.size() > 0) {
return bindings.get(0).getProvider().get();
}
return null;
}
}
So, basically I want:
- a JpaStore<Project, Query<Project>> instance for Project.class
- a JpaStore<ProjectGroup, Query<ProjectGroup>> instance for
ProjectGroup.class
- so on ...
My question - How can I do such an instance retrieval using Guice API
at runtime?
Thanks,
Rahul
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---