In your example, the log entry would get overwritten each time. With a
provider you'd get a new entry each time.

Dhanji.

On Fri, Jul 23, 2010 at 5:10 AM, Himsit <[email protected]> wrote:

> At
> http://code.google.com/docreader/#p=google-guice&s=google-guice&t=InjectingProviders
> they say:
>
> "With normal dependency injection, each type gets exactly *one
> instance* of each of its dependent types. The RealBillingService gets
> one CreditCardProcessor and one TransactionLog. When this flexibility
> is necessary, Guice binds a provider."
>
> But CreditCardProcessor and TransactionLog *are* two different types
> (and therefore only need one instance each). Why is this "flexibility"
> not "normal"?
>
> Furthermore, down the page under "Providers for Multiple Instances",
> they give this code:
>
> 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();
>    }
>  }
>
>
> Why does LogFileEntry need a provider? Wouldn't this work just fine
> without one:
>
> public class LogFileTransactionLog implements TransactionLog {
>
>  private final LogFileEntry logFileEntry;
>
>  @Inject
>  public LogFileTransactionLog(LogFileEntry logFileEntry) {
>    this.logFileEntry = logFileEntry;
>  }
>
>  public void logChargeResult(ChargeResult result) {
>    logFileEntry.setText("Charge " + (result.wasSuccessful() ?
> "success" : "failure"));
>    logFileEntry.save();
>
>    if (!result.wasSuccessful()) {
>      logFileEntry.setText("Failure result: " + result);
>      logFileEntry.save();
>    }
>  }
>
> What am I missing? Does each call to Provider.get() create a new
> instance of LogFileEntry? If so, how would you gather them up later?
>
> --
> 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]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>

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