It depends on when you want to the object to be available. For eager
loading, you inject the object. For lazy loading, you inject the provider.
Using a provider, the object would only be created only when you call get on
the provider implementation.

On Fri, Sep 4, 2009 at 6:22 AM, mcfly <[email protected]>wrote:

>
> sorry for pushing this...
>
> why do I give the provider and not the object of interest ? I'm
> confused about this and the Law of Demeter... Now the application has
> to get its object ...
>
> is it because the get method could do some magic behind that the
> returned objects are different instances or even different classes ?
>
> thanks
>
>
> On 15 Aug., 13:34, Dan Godfrey <[email protected]> wrote:
> > Guice will generate the provider for you automatically, based off the
> > binding to LogFileEntry.
> >
> > On Aug 14, 7:18 pm, Brandon Atkinson <[email protected]>
> > wrote:
> >
> > > The point of confusion might be that you're expecting Guice to generate
> a
> > > Provider.  It is you who has to write an implementing
> Provider<LogFileEntry>
> > > class.
> > > You then need to tell Guice to use a certain Provider when asked for a
> > > certain class.
> >
> > > For instance, you'd implement one Provider for clear text log files:
> >
> > > public class ClearTextLogFileEntryProvider implements
> Provider<LogFileEntry>
> > > {
> > >     public LogFileEntry get() {
> > >          return new ClearTestLogFileEntry();
> > >     }
> >
> > > }
> >
> > > or one for encrypted log files:
> >
> > > public class EncryptedLogFileEntryProvider implements
> Provider<LogFileEntry>
> > > {
> > >     public LogFileEntry get() {
> > >         return new EnctryptedLogFileEntry();
> > >     }
> >
> > > }
> >
> > > There is no magic here, so you have to tell Guice when to use which
> > > Provider:
> >
> > > Guice.createInjector(new Module() {
> > >     public void configure(Binder binder) {
> >
> > >
>  binder.bind(LogFileEntry.class).toProvider(ClearTextLogFileProvider.class);
> > > //use clear text logs
> > >     }
> >
> > > });
> >
> > > With the above binding, you're telling Guice that the
> > > ClearTextLogFileProvider.class is the source of LogFileEntry's.  When
> the
> > > Injector encounters a class requiring a LogFileEntry or
> > > LogFileEntryProvider, it will use ClearTextLogFileProvider.  For
> instance:
> >
> > > public class SomethingInteresting {
> > >    private Provider<LogFileEntry> provider;
> > >    private LogFileEntry log;
> >
> > >     /*
> > >      * The ClearTextLogFileProvider.class would be used
> > >      * when injecting both of the following dependencies
> > >      */
> > >    @Inject SomethingInteresting( Provider<LogFileEntry>
> > > logFileEntryProvider, LogFileEntry log) {
> > >         this.provider = logFileEntryProvider;
> > >         this.log = log;
> > >     }
> >
> > > }
> >
> > > Hope that helps.
> >
> > > -Brandon
> >
> > > On Fri, Aug 14, 2009 at 3:47 AM, Anthony MULLER <
> [email protected]>wrote:
> >
> > > > The "Provider<LogFileEntry>" instance is injected in the constructor
> and
> > > > set as an attribute of the LogFileTransactionLog object.
> >
> > > > After, that depends on how is implemented the provider itself. When
> get()
> > > > method is called, the provider implementation can return a new
> instance
> > > > LogFileEntry, or always the same (quite strange in this case ?)
> >
> > > > What's your problem about that?
> >
> > > > Anthony MÜLLER
> >
> > > > 2009/8/14 zhaoyi <[email protected]>
> >
> > > >> Can anyone help me on this? thanks.
> >
> > > >> On Aug 10, 2:38 am, zhaoyi <[email protected]> wrote:
> > > >> > I have read the article "Providers for multiple instances" in this
> > > >> > linkhttp://code.google.com/p/google-guice/wiki/InjectingProviders
> .
> > > >> > The example shown is:
> > > >> > public class LogFileTransactionLog implements TransactionLog {
> >
> > > >> >   private final Provider<LogFileEntry> logFileProvider;
> >
> > > >> >   @Inject
> > > >> >   public LogFileTransactionLog(Provider<LogFileEntry>
> logFileProvider)
> > > >> > {
> > > >> >     this.logFileProvider = logFileProvider;
> > > >> >   }
> >
> > > >> >   public void logChargeResult(ChargeResult result) {
> > > >> >     LogFileEntry summaryEntry = logFileProvider.get();
> > > >> >     summaryEntry.setText("Charge " + (result.wasSuccessful() ?
> > > >> > "success" : "failure"));
> > > >> >     summaryEntry.save();
> >
> > > >> >     if (!result.wasSuccessful()) {
> > > >> >       LogFileEntry detailEntry = logFileProvider.get();
> > > >> >       detailEntry.setText("Failure result: " + result);
> > > >> >       detailEntry.save();
> > > >> >     }
> > > >> >   }
> >
> > > >> > There are two places to call logFileProvider.get() method. Are
> they
> > > >> > return the same type of LogFileEntry? If they return different
> type of
> > > >> > LogFileEntry, how does logFileProvider know which type is
> requested?
> >
> > > >> > thanks.
> >
>

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