Alright, thanks Aleksey, this is clear!

Many thanks.

On Fri, May 22, 2009 at 07:10:33AM -0700, Aleksey Didik wrote:
> 
> Hi, ways are different, depends on what you want to have :)
> 
> If composites are predefined on the injector creation moment, may be
> better to use multibinding:
> 
>  interface A { void make();}
> 
>     static class B implements A {
>         public void make() {}
>     }
> 
>     static class CompositeA implements A {
>         Collection<A> composites;
> 
>         @Inject
>         CompositeA(Set<A> composites) {
>             this.composites = composites;
>         }
> 
>         @Override
>         public void make() {
>             for (A composite : composites) {
>                 composite.make();
>             }
>         }
>     }
> 
>     public static void main(String[] args) {
>         Injector inj = Guice.createInjector(new AbstractModule() {
>             @Override
>             protected void configure() {
>                 bind(A.class).to(CompositeA.class);
>                 final Multibinder<A> multibinder =
> Multibinder.newSetBinder(binder(), A.class);
>                 multibinder.addBinding().to(B.class);
>                 multibinder.addBinding().toInstance(new B());
>             }
>         });
>     }
> 
> Set of A will be injected to CompositeA by Guice.
> But it's hardly predefined composites and  only Set could be used.
> But composites will be injected by Guice, not by hand.
> More here: http://code.google.com/p/google-guice/wiki/Multibindings
> 
> If composites can be changed in runtime, @Provides method or custom
> provider is the best one.
> 
> Good luck,
> Aleksey.
> 
> 
> On May 22, 5:59 pm, Jelle Herold <[email protected]> wrote:
> > Hi List,
> >
> > First: thanks for this fantastic project, it is really well designed!
> >
> > One question, I have a composite object, like so
> >
> >         interface A { do(); }
> >
> >         class B implements A { do() { ... } }
> >
> >         class CompositeA implements A {
> >                 List<A> composites = ...
> >                 add(A a) { composites.add(a) };
> >                 do() {
> >                         for(A a : composites)
> >                                 a.do();
> >                 }
> >         }
> >
> > What would be the best way to inject this?
> >
> > Right know I configure it in my Module using @Provides, like so
> >
> >         @Provides A provideA() {
> >                 final CompositeA a = new CompositeA();
> >                 a.add(new B());
> >                 a.add(new B());
> >                 return a;
> >         }
> >
> > Although this works, it feels like I should be doing this differently.
> >
> > Please comment,
> >
> > Thanks for you time.
> > Jelle.
> >
> >  signature.asc
> > < 1KViewDownload
> > 

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to