This is a prime use case to bind the Portfolio in
RequestScope<http://code.google.com/p/google-guice/wiki/ServletModule#Using_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

On Tue, Sep 6, 2011 at 4:46 PM, Mark Nuttall-Smith <
[email protected]> wrote:

> 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
>
>  --
> 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 [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.
>

-- 
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.

Reply via email to