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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to