In general lean against that type of design in favor of designing a contextual aware app based on Scopes <https://github.com/google/guice/wiki/Scopes>, or by using ephemeral scopes with AssistedInject <https://github.com/google/guice/wiki/AssistedInject>. One reason is, if your app is a long running process, then you don't want the expense of creating an Injector with each request.
Fred On Sun, Mar 6, 2016 at 2:43 PM, Mashrur Mia <[email protected]> wrote: > Hi All, > > Checking out Guice and I love it. I currently have problem where guice > solved it by injecting all the required dependencies I need. But I wonder > if I am using Guice in the wrong way. What I require though is define > bindings depending on specific instance. And to achieve this I passed the > instance in the module. > > For instance, consider the following (somewhat similar to my problem): > > > public class CustomerModule extends AbstractModule { > private Customer customer; > > public CustomerModule(Customer customer){ > this.customer = customer; > } > > @Override > public void configure() { > bind(ReportGenerator.class).to(HtmlReportGenerator.class); > } > > @Provides > Account providePurchasingAccount() { > return customer.getPurchasingAccount(); > } > } > > > And I use this module to get Account dependency injected to the report > generator class that needs the account of a specific customer. For example, > a user chooses a specific customer and say, wants to show a generated > report. I have method like > > > public void printReport (Customer customer){ > Injector injector = Guice.createInjector(new CustomerModule(customer)); > ReportGenerator reportGenerator = injector.getInstance(ReportGenerator > .class); > > showReport(reportGenerator.generate()) > } > > > > Once the work is done, I am done with this module. > > Is this a ok use of guice? > > BTW the above example was inspired by > https://github.com/google/guice/wiki/InjectOnlyDirectDependencies but > perhaps my related question is, how does the "providePurchasingAccount" > gets a specific Customer? > > thanks for you help guys! > Mashrur > > > > > > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/google-guice. > To view this discussion on the web visit > https://groups.google.com/d/msgid/google-guice/6ccc3524-3592-48ca-ad14-cdeb8ff81a77%40googlegroups.com > <https://groups.google.com/d/msgid/google-guice/6ccc3524-3592-48ca-ad14-cdeb8ff81a77%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAKCukAJqXE6ykBh_5axXz5ciS4qrZxWxepi%2B1xCbAhCLdZVm4g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
