We have a project where we use GWT, with the MVP paradigm, and Gin. We have one presenter, but now I want to have different views (one for normal users, and one for the administrator, who sees almost the same, except for an extra column and different texts). The problem is that I want to select the view I want to use at runtime, because only then I know the userlevel.
I found the GinMapBinder, which seems to do what I want. In the code is an example on how to instantiate it, but not on how to use it.
So my questions: Is this the way to go and solve my problems? Can anybody give an example explaining how to use this and how to select the different injections at runtime?
GinMapBinder
allows you to incrementally build a binding to ajava.util.Map
, so the way to use it is to get aMap
injected into your object. You'll find a complete example in Guice'sMapBinder
javadoc,GinMapBinder
works the same.To answer your questions:
It could: if you inject a
Map<String, Provider<MyView>>
with the keys being the user's role. If you have only two such roles, you also simply inject twoProvider
s, and chose the one toget()
depending on the user's role. (see also below)That's not how I'd do it though. I'd rather use a combination of deferred-binding (to generate distinct permutations for normal and admin users), with a property-provider to choose the right permutation at runtime, and dynamic host page (to pass the admin information from the server to the client before the client starts).
You'd use deferred-binding to choose which Ginjector to use (using a factory and
<replace-with>
rules); Ginjectors would be identical (same methods, inherited from a base interface) except for the@GinModules
; and thus you can have aGinModule
for normal users and another one for admin users, each binding theMyView.class
to a distinct implementation class.Build a map binding user roles to view implementations:
Then inject it along with the user's role:
And choose the appropriate view depending on the user's role:
The alternative I was talking about: