Agree with Scott on both the concerns and the implementation. If your next question is going to be, "but I don't know if it's one or more of the dependent objects inside the parent", use Providers. I am assuming either the parent 'serviceconsumer' or some other object 'fills' the dependencies and you can use Providers to create the objects as needed. Yeah, it should work similarly in GIN.
On Dec 20, 12:25 pm, scott <[email protected]> wrote: > Can't say I completely understand the object graph you're trying to > create, sounds essentially like ServiceConsumers can depend on other > ServiceConsumers. > > I personally prefer using constructor injection, and putting @Inject > on a method like getDependencies() seems weird to me, especially since > it's part of the interface. It's not always possible to use > constructor injection if another framework is creating your objects, > but for your example I'll assume it is possible. > > If you use constructor injection and inject > CustomerAccountsServiceConsumer, and CustomerAccountsServiceConsumer > into CustomerServiceConsumer then you can implement the > getDependencies() yourself: > > class CustomerServiceConsumer extends ServiceConsumer { > CustomerAccountsServiceConsumer accountConsumer; > CustomerCreditCardsServiceConsumer creditCardConsumer; > > @Inject > CustomerServiceConsumer(CustomerAccountsServiceConsumer > accountConsumer, CustomerCreditCardsServiceConsumer > creditCardConsumer) { > this.accountConsumer = accountConsumer; > this.creditCardConsumer = creditCardConsumer; > } > > @Override > public List<ServiceConsumer> getDependencies() { > return Arrays.asList(new ServiceConsumer[] { accountConsumer, > creditCardConsumer } ); > } > > Using default bindings, when you ask Guice to create an instance of > CustomerServiceConsumer, it will first create a new > CustomerAccountsServiceConsumer and then a new > CustomerCreditCardsServiceConsumer and pass those instances into the > constructor of CustomerServiceConsumer. > > -Scott > > On Dec 19, 4:18 am, Aladdin <[email protected]> wrote: > > > > > > > > > Hi , I'm new to the concept of IoC so I hope my question is not funny > > and I would appreciated that when people answer me provide full code. > > Now I have an Abstract class called ServiceConsumer. Assuming I have > > the following > > Say this is the ServiceConsumer class > > public abstract class ServiceConsumer { > > public abstract List<ServiceConsumer> getDependencies(); > > public abstract Object consum(); > > > } > > > And those are the concrete versions : > > > 1-CustomerServiceConsumer > > 2-CustomerAccountsServiceConsumer > > 3-CustomerCreditCardsServiceConsumer > > > Some how I want inject CustomerAccountsServiceConsumer & > > CustomerCreditCardsServiceConsumer into getDependencies() in > > CustomerServiceConsumer > > > Can this be done ? > > > * I also want to note that I'm using GIN , not Guice but I think they > > have the same concept . > > > Thanks in advance > > > Aladdin -- 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.
