Hi Robbie,
Thanks for your reply.
I thought about that. But That would mean creating a StatesLoader as
DAO to query the database and a factory impl to return an instance of
DAO, which in turn would return a Map<String, String> if I were to use
good coding practices. Only then would I be able to inject the Map
object. My intention behind using GUICE was to avoid using a factory
implementation.
For the second option, I did not understand. Are you referring to an
implementation something like this? If yes, I would have instantiate
the Map<String, String> object before binding to a provider.
public class StatesProvider implements Provider<Map<String, String>> {
private Map<String, String> states;
@Inject
public StatesProvider(Map<String, String> states) {
this.states = states;
}
public Map<String, String> get() {
return this.states;
}
}
Again a TypeLiteral object requires an instance to be initialized. I
wanted to do something like this in GUICE, and using a example from
Spring's framework
public class StatesLoader extends ApplicationObjectSupport {
private static Map<String, String> states = new HashMap<String,
String>();
@Override
protected void initApplicationContext() {
// Populate {@link #states} after querying the database.
}
public static Map<String, String> getStates() {
return states;
}
}
On Apr 28, 10:27 am, "Robbie Vanbrabant" <
robbie.vanbrab...@gmail.com>
wrote:
> What do you mean "it takes a userId and loads all the US states"? I guess
> the US states are the same for every user and that they don't change that
> often.
>
> Anyway, I suppose you want to have that Map<String, String> as a singleton
> provided by Guice. Some quick ideas:
> - Populate a Map before you create the Injector, and bind it toInstance
> - Bind to a Provider as an eager singleton that queries the states and
> returns a Map
> - ...
>
> Remember to bind that Map<String,String> using a TypeLiteral.
>
> Cheers
> Robbie
>
> On Mon, Apr 28, 2008 at 7:07 PM, Karthik Krishnan <
Krishnan.1...@gmail.com>