Ya, it already does that, which is nice. But, there's some cases where we
want the Provider to have a bit more complexity to it, and binding to those
providers requires a bit of annoying code right now. It'd be nice to be
able to reuse the simplified @Provides syntax and return those Providers
directly. Essentially it'd be a reverse Provider method. Instead of Guice
creating a Provider implementation that calls a method, Guice would be
binding to the Provider implementation supplied by the method.
For example, we have a large settings framework where each setting
implements Provider<T>. I'd like to be able to bind those methods with a
simple: @Provides @MyBindingAnnotation Foo methodName() { return
myFooSetting; }, rather than
bind(Foo.class).annotatedWith(MyBindingAnnotation.class).toProvider(myFooSetting);.
In this example, it's just syntactic sugar, but when the Provider requires
dependencies, it can reduce a whole boilerplate of code.
Sam
On Tue, Sep 15, 2009 at 11:54 AM, Robbie Vanbrabant <
[email protected]> wrote:
> Doesn't @Provides work by wrapping those method invocations with a Provider
> instance? If you do @Provides Foo you will already be able to inject a
> provider for Foo. Not sure if that is what you mean.
>
> Robbie
>
>
> On Tue, Sep 15, 2009 at 5:35 PM, Sam Berlin <[email protected]> wrote:
>
>> Hi Folks,
>>
>> It's currently not possible to have an @Provides Provider<Foo> method in a
>> Module (Guice throws an exception saying binding to Provider is not
>> allowed). While technically this is achievable by having a class implement
>> Provider & binding to the class, I find the shorthand of @Provides to be
>> much much easier & simpler (with parameters passed into the method as
>> dependencies, etc..). Would it be possible to relax that restriction and
>> allow a binding to a Provider act the same as bind(..).toProvider(...)?
>>
>> Thanks!
>>
>> Sam
>>
>>
>>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---