Guice noob here.
I'm writing a JDO web application on AppEngine and am deciding how to
scope my service classes relative to Guice. One such service will be a
UserManager that handles, among other things, authentication of users.
Most of the best practices I've seen out there show a class like
UserManager scoped as Singleton, but having a Singleton invites
concurrency issues that I may not want to deal with and requires me to
use providers rather than the objects themselves in my implementation.
It seems like if I RequestScoped my UserManager implementation I
would reap some benefits:
(1) I wouldn't have to deal with concurrency issues
(2) I could safely inject any @RequestScoped, @SessionScoped, or
@Singleton variables into my service class without going through
Provider<>s. Even though my Servlets/Filters would now need
Provider<>s for these services, it would result in more elegant code
within classes like UserManager.
I seems reasonable as long as I can provide that service with a
PersistenceManager inexpensively, which I can do.
Is there any reason I shouldn't do this? The only reason I can think
of are possible construction costs of the Manager with each request,
but that doesnt seem particularly expensive.
Here's an example of doing an authentication servlet with a
RequestScoped UserManager service class. I'm certain there are many
syntax errors.
public interface UserManager {
public boolean authenticate();
}
@RequestScoped
class JDOUserManager implements UserManager {
//RequestScoped w/ Singleton factory. Cheap.
@Inject private final PersistenceManager pm;
//SessionScoped
@Inject private final User user;
@Override
public boolean authenticate {
//test user against db
}
}
@RequestScoped //avoid Providers w/ RequestScoped + ServletAdapter
public class AuthenticationServlet extends HttpServlet {
@Inject private final UserManager manager;
@Inject private final User user;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse
resp) {
if(!user.isLoggedIn()) {
user.setUsername(req.getParameter("user"));
user.setPassword(req.getParameter("password"));
if(manager.authenticate()) {
user.setLoggedIn(true);
//let them into the site
} else {
//back to the login page with you
}
} else {
//let them into the site
}
}
}
That's it! Thanks for all the help...
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---