This specific use case feels more like business logic than injection. The 
Customer and Account both appear to be request scoped business objects that 
should be looked up from places like the database and passed to services. 

Sent from my iPhone

> On Mar 6, 2016, at 3:57 PM, Fred Faber <ffa...@faiser.com> wrote:
> 
> In general lean against that type of design in favor of designing a 
> contextual aware app based on Scopes, or by using ephemeral scopes with 
> 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 <mashrur....@gmail.com> 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 google-guice+unsubscr...@googlegroups.com.
>> To post to this group, send email to google-guice@googlegroups.com.
>> 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.
> 
> -- 
> 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 google-guice+unsubscr...@googlegroups.com.
> To post to this group, send email to google-guice@googlegroups.com.
> 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.

-- 
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 google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
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/7946D2B0-9372-47E5-99C3-EC3F556B9772%40pontarelli.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to