Hi, Marcel,
If I understood the problem correctly, the following solution might
work.
Instead of maintaining a list of SpecialThings and the *active*
SpecialThing separately, I suggest that you create a
SpecialThingService service interface with a singleton implementation.
This service will manage the states of both the list and the active
SpecialThing. For example:
public interface SpecialThingService {
SpecialThing getActiveSpecialThing();
List<SpecialThing> listSpecialThings();
}
Now you can either @Inject SpecialThingService into other classes that
work with special things, e.g.:
...
@Inject
private SpecialThingService sts;
...
SpecialThing active = sts.getActiveSpecialThing();
...
List<SpecialThing> stList = sts.listSpecialThings();
...
Or, if you prefer a provider for the active special thing, you can:
public class ActiveSpecialThingProvider implements
Provider<SpecialThing> {
@Inject
private SpecialThingService sts;
public SpecialThing get() {
return sts.getActiveSpecialThing();
}
}
Then:
...
@Inject
Provider<SpecialThing> activeSpecialThing
...
Cheers,
Yegor
On Jan 14, 1: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].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.