hello,

I think you can get what you need using a simple factory instead of a
vanilla Provider:

interface DependentOnSpecialFactory {
  DependentOnSpecial create(SpecialThing currentSpecialThing);
}

and then it sounds like you can simply use a FactoryProvider to bind this:

public class YourModule extends AbstractModule {


  @Override
   protected void configure() {
      // ....whatever else you do
      bind(DependentOnSpecialFactory.class)
         .toProvider(FactoryProvider.newFactoryProvider(
               DependentOnSpecialFactory.class, DependentOnSpecial.class)));
   }
}


You could do a similar thing with Scopes, but for such tightly coupled
creation logic of:
1) identifying the current SpecialThing
2) effectively passing it to the Provider (in your model) to create an
instance of DependentOnSpecial

then I think a straightforward factory is clearer.

-Fred

On Thu, Jan 14, 2010 at 3:06 AM, Marcel <[email protected]> wrote:

> Hi, I'm new to Guice and I'm trying to convert an existing application
> to use guice. Most parts work quite fine, but for one problem I didn't
> find a nice solution so far.
>
> I created a little example that hopefully illustrates my problem. I
> have a class that depends on a list of SpecialThings and a provider
> that itself depends on the *currently active* SpecialThing, i.e., the
> provider needs to know what SpecialThing is active when calling get()
> because this affects its return value:
>
>    @Inject
>    public TestExample(final List<SpecialThing> specials, final
> Provider<DependentOnSpecial> provider)
>    {
>        this.specials = specials;
>        this.provider = provider;
>    }
>
>
>
>    public void run()
>    {
>        for (final Special currentSpecial : specials)
>        {
>            final DependentOnSpecial dep = provider.get();
>            // provider.setSepcialThing(special); // ???
>            for (final String string : dep.elements())
>            {
>                System.out.println(string);
>            }
>        }
>    }
>
> However, I think this is not the right way how to do it in Guice,
> right? I thought about using my own scope but I don't didn't
> understand how I can configure the scope with a particular instance of
> SpecialThing. SpecialThing is itself not free of dependencies, i.e.,
> it cannot be created using no-scope.
>
> Sorry, I got lost in my dependencies somehow :-}
>
> Summarized:
> I have a singleton List<SpecialThings>.
> I have Components that depend on the currently active, SpecialThing,
> i.e., the Injector should inject this active instance to the
> providers.
>
> It sounds like needing a scope but how can I achieve this notion of
> *active*  SpecialThing from singleton list?
> Thanks in Advance.
>
> --
> 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]<google-guice%[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