On Sep 14, 8:38 pm, Gili <[email protected]> wrote:
>      So users instantiate StatelessClass using
> StatelessClassFactory.getInstance() which uses an Injector under the
> hood. They use "new StatefulClass()" to get an instance of
> StatefulClass. But then what happens if StatefulClass needs to
> reference StatelessClass? If I use StatelessClassFactory it makes
> StatefulClass harder to unit-test, but what's the alternative?

I recommend you supply library-users with Guice Modules! And if
they're strangely afraid of Guice, give them a bridge API that wraps
an Injector. It can expose getters for your public types, including
any public factories (assisted inject-implemented or otherwise).

  class MyLibrary {
    private final Injector injector;
    public MyLibrary() {
      injector = ...;
    }
    public Foo getFoo() {
      return injector.getInstance(Foo.class);
    }
    public Bar getBar() {
      return injector.getInstance(Bar.class);
    }
    public BazFactory getBazFactory() {
      return injector.getInstance(BazFactory.class);
    }
  }

I wouldn't bother wrapping this stuff in interfaces for testability.
If your users are afraid of Guice or dependency injection, they are
almost certainly also afraid of testing!
--~--~---------~--~----~------------~-------~--~----~
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