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
Thanks for your reply.
Do you know if it's a common uses to have more than 20 privates modules ?
--To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/bqRh1CKDwsEJ.
You received this message because you are subscribed to the Google Groups "google-guice" group.
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.
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;
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/jH07TWjWEZ8J.
@Retention(RetentionPolicy.RUNTIME) @BindingAnnotation public @interface DoesFooStuff { FooClient value();
enum WhatKindOfStuff {
STUFF_THAT_A_WANTS,
STUFF_THAT_B_WANTS,
...
STUFF_THAT_Z_WANTS
}
}
You would then use this to annotate your Foos:
FooClient class AFoo {
@Inject AFoo(@DoesStuffForFoo(Foo.WhatKindOfStuff.STUFF_THAT_A_WANTS) IFoo foo) {
...
}
}
In your module you'd define an implementation of the interface (which is tricky...you need to be careful to follow the spec on the Annotation javadoc):
@SuppressWarnings("ClassExplicitlyAnnotation") private static class DoesFooStuffImpl implements DoesStuffForFoo {
private final WhatKindOfStuff value;
private DoesFooStuffImpl(WhatKindOfStuff value) {
this.value = value; } @Override WhatKindOfStuff String value() {
return value; } @Override public Class<? extends Annotation> annotationType() { return DoesStuffForFoo.class;
} @Override public String toString() { return "@" + DoesStuffForFoo.class.getName() + "(value=" + value + ")";
} @Override public boolean equals(Object o) { return o instanceof DoesStuffForFooImpl
&& ((DoesStuffForFoo) o).value().equals(value());
} @Override public int hashCode() { return (127 * "value".hashCode()) ^ value.hashCode(); } }
You could then define a helper method as syntatic sugar over the binding:
private void bindFoo(Class<? extends Foo> fooClass, WhatKindOfStuff whatKindOfStuff {
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:
A pattern to mitigate the boilerplate is to use a parameterized annotation:
@Retention(RetentionPolicy.RUNTIME) @BindingAnnotation public @interface DoesFooStuff { DoesFooStuff value();
enum WhatKindOfStuff {STUFF_THAT_A_WANTS,STUFF_THAT_B_WANTS,...STUFF_THAT_Z_WANTS
}}You would then use this to annotate your Foos:
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/1JhF9-nY1McJ.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/DMiQb3afXlYJ.