Hi,

        Okay, but who injects the factories in the first place? I was under the 
impression that users invoke:

public static Stateless SomeFactory.getStateless();

        I assume you had something else in mind?

Gili

Logan Johnson wrote:
> 
> Pretty sure you can just ask for the Injector in your factory:
> 
> class SomeFactory {
>  @Inject Injector guice;
> }
> 
> I've typically injected the Provider instead:
> 
> class SomeFactory {
>    @Inject Provider<SomeService> provider;
> }
> 
> The bad news is this is kind of annoying boilerplate.  The good news is 
> that you should have to do it very infrequently, if your API has low 
> surface area.
> 
> 
> 
> On Tue, Sep 15, 2009 at 10:00 AM, Gili Tzabari <[email protected] 
> <mailto:[email protected]>> wrote:
> 
> 
>     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]>
>      > <mailto:[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