You could consider writing a provider method for those classes where you want 
to use the non-default binding.

For example, assuming you use constructor injection:

   public void configure()
   {
      bind( IFoo.class ).to( DefaultFoo.class );
   }

   @Provides
   public E provideE( MyEFoo foo, IBar bar, ...etc... )
   {
      return new E( foo, bar, ...etc... );
   }

   @Provides
   public K provideK( MyKFoo foo, IBar bar, ...etc... )
   {
      return new K( foo, bar, ...etc... );
   }

The downside of this approach is that because you're constructing the instance, 
it can't be intercepted by Guice (ie. for AOP).

Full example below...

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import javax.inject.Inject;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provides;

interface IFoo
{
}

interface IBar
{
}

class DefaultFoo
    implements IFoo
{
}

class DefaultBar
    implements IBar
{
}

class MyBFoo
    implements IFoo
{
}

class A
{
    @Inject
    public A( IFoo foo, IBar bar )
    {
        System.out.println( "Got " + foo + " and " + bar );
    }
}

class B
{
    @Inject
    public B( IFoo foo, IBar bar )
    {
        System.out.println( "Got " + foo + " and " + bar );
    }
}

class C
{
    @Inject
    public C( IFoo foo, IBar bar )
    {
        System.out.println( "Got " + foo + " and " + bar );
    }
}

class ExampleModule
    extends AbstractModule
{
    @Override
    protected void configure()
    {
        bind( IFoo.class ).to( DefaultFoo.class );
        bind( IBar.class ).to( DefaultBar.class );
    }

    @Provides
    B provideB( MyBFoo foo, IBar bar )
    {
        return new B( foo, bar );
    }
}

public class Example
{
    public static void main( String[] args )
    {
        Injector injector = Guice.createInjector( new ExampleModule() );

        System.out.println( "--- A ---" );
        injector.getInstance( A.class );
        System.out.println( "--- B ---" );
        injector.getInstance( B.class );
        System.out.println( "--- C ---" );
        injector.getInstance( C.class );
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On 7 Sep 2012, at 08:02, robertdup wrote:

> Yes it's right.. but this way don't satisfy me totally cause I have some XFoo 
> classes.
> I will have to create many annotation (@A,@B, ..., @Z), and I must bind all 
> of them or I will get a guice exception.
> 
> bind(IFoo.class).annotatedWith.(A.class).to(DefaultFoo.class);
> bind(IFoo.class).annotatedWith.(B.class).to(DefaultFoo.class);
> [...]
> bind(IFoo.class).annotatedWith.(Y.class).to(DefaultFoo.class);
> bind(IFoo.class).annotatedWith.(Z.class).to(DefaultFoo.class);
> 
> In my case, I just have to override a couple of binding and let the other on 
> the default implementation (DefaultFoo.class)
> bind(IFoo.class).annotatedWith.(E.class).to(MyEFoo.class);
> bind(IFoo.class).annotatedWith.(K.class).to(MyKFoo.class);
> 
> Otherwise, PrivateModule looks too "heavy" to implement in my case..
> 
> Well, Is there an other way that could be less verbose ?
> 
> Best regards;
> 
> On Friday, September 7, 2012 3:59:12 AM UTC+2, Fred Faber wrote:
> Strictly speaking, the robot legs problem describes a scenario where the 
> types of your object chain are identical.  In your case, you wouldn't have 
> AFoo and BFoo, but just Foo.  It's a luxury of sorts to have AFoo and BFoo 
> because you _can_ use a binding annotation on the constructor of each.  That 
> is the solution I would prefer for its clarity in how AFoo and BFoo are being 
> configured:
> 
> class AFoo {
>   @Inject AFoo(@DoesStuffRelatedToA IFoo ifoo) { ... }
> }
> 
> class BFoo {
>   @Inject BFoo(@DoesStuffRelatedToB IFoo ifoo) { ... }
> }
> 
> Here, grepping through the code directly for DoesStuffRelatedToA would lead 
> me to the binding for IFoo in context of AFoo, and that's a little bit of a 
> win for code maintenance.
> 
> Fred
> 
> On Thu, Sep 6, 2012 at 12:51 PM, robertdup <[email protected]> wrote:
> Thanks for your reply. 
> 
> Do you know if it's a common uses to have more than 20 privates modules ?
> 
> 
> 
> On Thursday, September 6, 2012 4:00:35 PM UTC+2, Thomas Broyer wrote:
> This is known as the "robot legs" problem, see 
> http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions#How_do_I_build_two_similar_but_slightly_different_trees_of_objec
> 
> On Thursday, September 6, 2012 9:14:24 AM UTC+2, robertdup wrote:
> Hello there,
> 
> I trying to implement default binding on my module without any success...
> 
> Here is what I would like to do (my dream) :
> 
> class AFoo
> {
>     @Inject AFoo( IFoo foo ){}
> }
> 
> class BFoo
> {
>    @Inject BFoo( IFoo foo ){}
> }
> 
> 
> bind(IFoo.class).to(DefaultFoo.class);
> bind(IFoo.class).to(OtherFoo.class).on(BFoo.class);
> 
> 
> I know that I could solve this problem using annotation like this :
> 
> class AFoo
> {
>     @Inject AFoo( @A IFoo foo ){}
> }
> 
> class BFoo
> {
>    @Inject BFoo( @B IFoo foo ){}
> }
> 
> 
> bind(IFoo.class).annotatedWith(A.class).to(DefaultFoo.class);
> bind(IFoo.class).annotatedWith(B.class).to(OtherFoo.class);
> 
> But this way is too boring and dirty.. (because I have to add 
> annotation/binding definition for each one)
> 
> Are there some others ways to solve Default binding "problem" ?
> Thanks in advance; Best regards
> 
> 
> -- 
> 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/-/bqRh1CKDwsEJ.
> 
> 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.
> 
> 
> -- 
> 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/-/jH07TWjWEZ8J.
> 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.

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