good idea / bad idea: CollectionBindingBuilder

5 views
Skip to first unread message

Logan Johnson

unread,
Mar 31, 2008, 9:59:34 PM3/31/08
to google...@googlegroups.com
Some months ago, I thought it would be nice to be able to directly bind a Collection with Guice-created elements.  That turned out to be a bad idea for my project, but I've had the first hour or two of a proof of concept sitting in Eclipse ever since.

I took another step on that proof of concept today, and more or less successfully got a CollectionBindingBuilder to work:

b.bindSetOf(StringHolder.class).toElements(StringHolder1.class, StringHolder2.class);

StringHolder1 and StringHolder2 are implementations of StringHolder, and are created and injected by Guice as normal.  A Set<StringHolder> is constructed and populated with the Guice-created StringHolder1 and StringHolder2 instances, and then injected into a client class.

This is all pretty rough and untested at the moment, and would require a lot of work to complete.  I say it "more or less" works because there's a warning about a generic array creation for the varargs in the binding line above.

So, now that I know it can be done but before I turn a fun trick into a major project, what does everyone think of the idea?  Is this kind of collection injection a feature anyone would particularly like to see, or particularly prefer *not* to see?

--Logan


Josh McDonald

unread,
Mar 31, 2008, 10:10:20 PM3/31/08
to google...@googlegroups.com
I think it's kinda cool, but how often is that going to come up? ;-)
--
"Therefore, send not to know For whom the bell tolls, It tolls for thee."

:: Josh 'G-Funk' McDonald
:: 0437 221 380 :: jo...@gfunk007.com

Logan Johnson

unread,
Mar 31, 2008, 10:12:40 PM3/31/08
to google...@googlegroups.com
Not often, I'll bet.  Like I said, I had a use case but it turned out to be kind of stupid. :)  I'm curious as to whether anyone else has one burning a hole in their project.

Eric Anderson

unread,
Mar 31, 2008, 10:18:32 PM3/31/08
to google...@googlegroups.com
Actually,

If it was a List rather than a Set (I need the ordering), I would use it a good deal on our projects.  We frequently inject things like List<CustomValidator>.  We typically are order dependent on validations since earlier validations filter out elements in our input data that would cause NPE in later validations.  If you need any looking at this, I would be happy to look at a patch.

Eric

Martin Lukasiewycz

unread,
Mar 31, 2008, 11:05:50 PM3/31/08
to google-guice
I thought this feature is already known as "multibindings" and part of
the next version:
http://code.google.com/p/google-guice/issues/detail?id=37&sort=priority%20milestone

Logan Johnson

unread,
Mar 31, 2008, 11:23:34 PM3/31/08
to google...@googlegroups.com
Yeah, I'd pretty much forgotten about issue 37.  It's been a while since there's been any traffic on it, and I don't know that any decision was reached as to what multibindings would look like, or any code yet written.  I guess this is the beginning of an implementation of the collection-builder version.  There are other approaches discussed in that issue, like individual element contribution and adder injection.

--Logan
Reply all
Reply to author
Forward
0 new messages