Hi Logan,

        Good point!

        I think I am going to try a dual model:

1) Expose Guice in the public API
2) Provide a bridge layer that wraps Guice in factory classes

        Then depending on the demand I will either keep or drop #2 over time.

        How do you implement the factories though? Do you do the following?

class InjectorProvider
{
        private static final ThreadLocal<Injector> injector = new 
ThreadLocal<Injector>();

        public static Injector get()
        {
                return injector.get();
        }
}

class StatefulFactory
{
        public void doSomething()
        {
                Injector injector = InjectorProvider.get();
                // do stuff
        }
}

        Or is there a better way?

Thanks,
Gili

Logan Johnson wrote:
> What's forcing you to expose StatefulClass' constructor?  Why not 
> provide a factory for StatefulClass as well?
> 
> 
> 
> On Mon, Sep 14, 2009 at 11:38 PM, Gili <[email protected] 
> <mailto:[email protected]>> wrote:
> 
> 
>     Hi,
> 
>     A few months ago I asked whether one should expose Guice in public
>     APIs or hide it. I was advised to use Guice under the hood (to improve
>     testability) and expose factory classes to end-users. Here is what
>     I've got so far:
> 
> 
>     /** For internal use */
>     class StatelessClass
>     {
>      @Inject
>      public StatelessClass(OtherClass other);
> 
>      public Integer toInteger(String text);
>     }
> 
>     /** For end-users */
>     class StatelessClassFactory
>     {
>      public static StatelessClass getInstance();
>     }
> 
>     /** For end-users */
>     class StatefulClass
>     {
>      public StatefulClass(String arg1, String arg2);
> 
>      public doSomething();
>     }
> 
>         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?
> 
>     Thanks,
>     Gili
> 
> 
> 
> > 


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