AssistedInject in a Servlet

49 views
Skip to first unread message

Mark Nuttall-Smith

unread,
Sep 6, 2011, 4:46:28 PM9/6/11
to google...@googlegroups.com
Hi,

I have an class with a parameterized constructor using AssistedInject as follows:

  @Inject
  Portfolio (QuoteDao dao, @Assisted Params params) {...}

I create an instance of this class in a command handling servlet:

  public class Handler {
    @Inject Strategy strategy;
    Result execute(Action action) {
      Portfolio portfolio = portfolioFactory.create(action.params);
      strategy.execute();
    }
  }
 
My uncertainty begins when I need to obtain a reference to the portfolio in other components of the system. Ideally I'd have it included as part of the constructor, but since it isn't available at injection time this isn't possible.

  public class Strategy {
    Portfolio portfolio; <-- how should I get this here?
    @Inject
    public Strategy(QuoteDao dao, ??) {...}

    public execute();
  }

The example is somewhat simplified - it's not possible to simply pass the portfolio as part of the execute() call.
 
I've thought through various solutions, but none seem ideal:
    * Inject a PortfolioProvider - but how does the provider get access to the portfolio?
    * Save the portfolio in the Handler against the logged in user (I'm using appengine), then use the logged in user to retrieve in a provider - but I would like the system to be able to handle anonymous users
    * Store the portfolio in a static ThreadLocal, access in a provider again - seems very clunky
    
Would appreciate it very much if someone could help me out here? I'm a newbie with Guice, so hopefully I'm missing something obvious...

Thanks,
Mark

Fred Faber

unread,
Sep 6, 2011, 6:25:06 PM9/6/11
to google...@googlegroups.com
This is a prime use case to bind the Portfolio in RequestScope, so that it is available through an injection of Provider<Portfolio> to other classes in your app.

Please follow-up with any questions on the use of request scoping.

Fred


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/xuNJJV0EeDoJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Mark Nuttall-Smith

unread,
Sep 6, 2011, 7:14:13 PM9/6/11
to google...@googlegroups.com, ffa...@faiser.com
Hi Fred,

Thanks for the answer. I read the RequestScope documentation, and it certainly seems like the right thing to do be doing - but when the request is encapsulated in a command (I'm using gwtp) I'm not sure how I can access the action.params object I need to build the Portfolio in a filter class, in order to seed the requestscope.

Is a filter what you would suggest I use? Is this really a question for the gwtp guys?

Thanks,
Mark

Fred Faber

unread,
Sep 6, 2011, 7:21:18 PM9/6/11
to google...@googlegroups.com
By nature of using ServletModule, you can have your Handler class injected with Provider<HttpServletRequest>.  Within execute(), call .get() on this provider, and then seed the scope by setting the request attribute.  This should handle the scoping.

Fred
Reply all
Reply to author
Forward
0 new messages