Newbie Question ! - dependencies objects

25 views
Skip to first unread message

Aladdin

unread,
Dec 19, 2011, 5:18:23 AM12/19/11
to google-guice
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

scott

unread,
Dec 20, 2011, 12:25:41 PM12/20/11
to google-guice
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

SandMan

unread,
Dec 20, 2011, 5:20:30 PM12/20/11
to google-guice
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.

Thomas Broyer

unread,
Dec 21, 2011, 4:19:48 AM12/21/11
to google...@googlegroups.com
As others, I'm not sure I understood your problem, but maybe you're looking for the multibindings extension:

MultiBinder<ServiceConsumer> multi = Multibinder.newSetBinder(binder(), ServiceConsumer.class);
multi.addBinding().to(CustomerAccountsServiceConsumer.class);
multi.addBinding().to(CustomCreditCardsServiceConsumer.class);

and then in CustomerServiceConsumer, @Inject a Set<ServiceConsumer> (and turn it into a List, using e.g. new ArrayList(set), to comply with your getDependencies return type).

Aladdin

unread,
Jan 8, 2012, 1:08:21 AM1/8/12
to google-guice
Thank you all for the great replies,

I wanted to use IoC concept to avoid having dependencies hard-coded in
my class (at least this what I thought).

Having something like :

CustomerServiceConsumer(CustomerAccountsServiceConsumer
accountConsumer, CustomerCreditCardsServiceConsumer
creditCardConsumer) { ... }

Makes little sense to me as the class have to change every time I need
to have more dependencies .

let me try to be more clear ,

public class CustomerServiceConsumer extends ServiceConsumer<Client>{

@Override
public void consume() {
// consume code for client goes here

// Then load dependencies
for (ServiceConsumer<?> d:getDependencies()){
d.consume();
}
}

@Inject
@Override
public List<ServiceConsumer<?>> getDependencies() {
// ...
}
}

Now I want the getDependencies() to return the other 2 (or more)
services and I want to be able to glue them outside the class
CustomerServiceConsumer .

@Thomas I think you MultiBinder is what I need but I'm using GIN and
it's not supported :(


Any other suggestions ?

Aladdin


On Dec 21 2011, 12:19 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> As others, I'm not sure I understood your problem, but maybe you're looking
> for the multibindings extension<http://code.google.com/p/google-guice/wiki/Multibindings>
> :

Thomas Broyer

unread,
Jan 8, 2012, 11:24:55 AM1/8/12
to google...@googlegroups.com


On Sunday, January 8, 2012 7:08:21 AM UTC+1, Aladdin wrote:
@Thomas I think you MultiBinder is what I need but I'm using GIN and
it's not supported :(


Any other suggestions ?

Could possibly be as simple as (in your GinModule):

@Provides Set<ServiceConsumer<?>> provideServiceConsumers(CustomerAccountsServiceConsumer accountConsumer, CustomerCreditCardsServiceConsumer creditCardConsumer) {
    return new HashSet(Array.asList(accountConsumer, creditCardConsumer));
}

The major (if not only) difference with MultiBinder is that all bindings have to be known and declared in the same GinModule (whereas MultiBinder can "incrementally" add to the Set from distinct modules).

Sam Berlin

unread,
Jan 8, 2012, 12:25:25 PM1/8/12
to google...@googlegroups.com
To be exact, I don't think all the bindings have to be declared in the same Gin module, but they do have to be known.  IE., in the above snippet, CustomerAccountsServiceConsumer & CustomerCreditCardsServiceConsumer  can be declared in any module, but one module must have the @Provides Set<ServiceConsumer<?>> and know about all the of them in order to provide it.

sam
 

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/pEypSi01Q-0J.

To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Reply all
Reply to author
Forward
0 new messages