Hi people,

Let's consider the following situation.

We have a service manager that handles the service specific operations
and is
unique per service.

class ServiceManager {
  private BackendClient backend;
  private String serviceId;

  @Inject public ServiceManager(BackendClient backend, String
serviceId) {
    this.backend = backend;
    this.serviceId = serviceId;
  }

  public boolean equals(Object obj) {
    //.....
    return Objects.equals(serviceId, ((ServiceManager)obj).serviceId);
  }

  public void handle() {
    // here are bussuiness logic
    backend.doThis1();
    backend.doThis2();
    // ...
  }
}

I want to have the collection of ServiceManager in one of the standard
scopes,
let it be singleton scope. How is it possible to implement this with
Guice?

The trivial implementation would be creating a SuperServiceManager
that aggregates
these ServiceManager classes.

class SuperServiceManager {
  private Map<String, ServiceManager> mngrs = Maps.newHashMap();
  private BackendClient backend;

  @Inject public SuperServiceManager(BackendClient backend) {
    this.backend = backend;
  }

  public ServiceManager getManager(String serviceId) {
    if (!mngrs.contains(serviceId))
    {
        mngrs.put(new ServiceManager(backend, serviceId);
    }
    return mngrs.get(serviceId);
  }
}

And then we have to place SuperServiceManager in required scope.
class MyModule extends AbstractModule {
  public void configure()
  {
    bind(SuperServiceManager.class).in(Scopes.SINGLETON);
  }
}

But it's the case where keyword "new" arise and some dependencies are
handled by hands,
like putting BackendClient. Maybe there is much more wise solution
using some kinda
general provider or one of Guice2 cool features.

I hope that problem explained by me in this post is frequent enough
that you'd solved it.

Thank you.


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