Fred, thanks for the reply.
I think in some cases it makes sense to let Guice return the correct object
given the current scope of the caller. I want a Localizer? I ask for one,
without the need to ask which scope I'm in.
Otherwise I guess I have to do something like :
------------
@Inject
public MyClass(@NotRequestScopedLocalizer Ilocalizer localizer)
{
....
}
------------
Which, in my opinion, is not as clear as always using :
------------
@Inject
public MyClass(Ilocalizer localizer)
{
....
}
------------
electrotype
On Sunday, February 24, 2013 8:19:28 PM UTC-5, Fred Faber wrote:
>
> Hello,
>
> There's nothing innate in Guice that will give you this behavior in a
> simpler way. This is largely intentional (if not somewhat contentious),
> with the rationale being that most code that would benefit from this is
> probably best rewritten in to be more explicitly aware of the context in
> which it's being called.
>
> Fred
>
> On Fri, Feb 22, 2013 at 8:55 PM, <[email protected] <javascript:>> wrote:
>
>> I'm using Guice-Servlet, and I have a Localizer object that is request
>> scoped: this Localizer is built using the locale from the request.
>>
>> But I also need a default localizer at some places *where I'm not in a
>> request*. This default Localizer would be a singleton created using the
>> default Locale of the application.
>>
>> I currently have a solution, but it involves catching an exception
>> (ProvisionException) to know if the current scope is "request". I'd like to
>> know if there is better way to determine the current scope, for the
>> provider to return the correct Localizer?
>>
>> My current code :
>>
>> ---------------------------------------------------
>>
>> protected void bindRequestScopedLocalizer()
>> {
>> bind(Key.get(ILocalizer.class,
>> RequestScopedLocalizer.class)).to(MyLocalizer.class).in(ServletScopes.REQUEST);
>> }
>>
>> @Provides
>> @Singleton
>> @DefaultLocalizer
>> public ILocalizer providesDefaultLocalizer()
>> {
>> Locale locale = Locale.getDefault();
>> TimeZone timeZone = TimeZone.getTimeZone("UTC");
>>
>> return new MyLocalizer(locale, timeZone);
>> }
>>
>> @Provides
>> protected ILocalizer providesLocalizer(Provider<HttpServletRequest>
>> requestProvider,
>> @RequestScopedLocalizer
>> Provider<ILocalizer> requestScopedLocalizerProvider,
>> @DefaultLocalizer ILocalizer
>> defaultLocalizer)
>> {
>> try
>> {
>> @SuppressWarnings("unused")
>> HttpServletRequest request = requestProvider.get();
>> }
>> // If we're not in a Request scope, we use the default Localizer.
>> catch(ProvisionException ex)
>> {
>> return defaultLocalizer;
>> }
>>
>> return requestScopedLocalizerProvider.get();
>> }
>>
>> ---------------------------------------------------
>>
>> Thanks in advance!
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "google-guice" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/google-guice?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.