Status: New
Owner: ----
New issue 467 by ericaro: [RFE] add a bindCollection(Class component) to
handle Component oriented programming
http://code.google.com/p/google-guice/issues/detail?id=467
When using component oriented programming, it is usual to work with a
collection of component instances, without any knowledge
of their actual type. This is so close to guice that a very little effort
could make it easy to mix both.
see attached an [http://sscce.org Short, Self Contained, Correct Example]
to handle components with guice.
this code just requires the addition of a single method in the
AbstractModule:
this "extra" method that this RFE is about, is for binding collection of
components using the full power of guice for each
particular instance.
here is the method
/**
* Bind Collection<C> to an instance that will contain every
instances
* that where binded to C.
*
* @return a ScopedBindingBuilder to continue configuring the
* Collection<C> binding
*/
protected <C> ScopedBindingBuilder bindCollectionOf(final Class<C>
componentType) {
// build the type literal that matches Collection<C> sadly, java does
not make it possible
TypeLiteral<Collection<C>> collectionType =
(TypeLiteral<Collection<C>>)
TypeLiteral.get(Types.newParameterizedType(Collection.class,
componentType));
// creates a provider of the corresponding type
Provider<Collection<C>> p = new
Provider<Collection<C>>() {
@Inject Injector injector;
public Collection<C> get() {
Collection<C> collection = new
ArrayList<C>();
for (Binding<C> b :
injector.findBindingsByType(TypeLiteral.get(componentType)))
collection.add(b.getProvider().get());
return collection;
}
};
// request that this class to be injected in order to
get the injector
requestInjection(p);
// bind the collection type to this provider
return bind(collectionType).toProvider(p);
}
this code is mainly a wrap of findBindingsByType.
here is how component initialization looks like after
The configure method of my module can now be
@Override protected void configure() {
//A component implements I
//B component implements I,J
//My own component configuration ( 100% guicy)
bind(I.class).annotatedWith(Names.named("a")).to(A.class);
bind(I.class).annotatedWith(Names.named("b")).to(B.class);
bind(J.class).annotatedWith(Names.named("b")).to(B.class);
// Simple guicy injection of components collection made possible by
previous method
bindCollectionOf(I.class).asEagerSingleton();
bindCollectionOf(J.class);
}
hope you'll like it.
Attachments:
InJectorTest.java 3.0 KB
--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" 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-dev?hl=en.