On 23 July 2010 03:10, 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? >
Providers let you get multiple instances of the same type*, for example say I need N copies of a log entry where N can vary according to the incoming request being processed I don't know N ahead of time, so I can't have multiple injection points (such as fields) - I need some sort of factory, which lets me request new instances as I need them - so you can think of a Provider as a kind of locator/factory You can also use them to defer creation of expensive objects, as in the second example - if you injected the actual type the object would be created when your class was created, but with a Provider you can inject that and only create the provided instance much later when you actually need it As for gathering them up - you could either use the Guice SPI to get notified of instances created by Guice, or you could use AOP, or you could write your own Provider, or you could gather them yourself as you use them (* not all Providers will return a new instance, some may return a previous instance depending on the scope of the binding ) -- > 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. > -- Cheers, Stuart -- 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.
