On 25 Nov 2013, at 5:51, Brian Pontarelli wrote:

Constructor injection uses a language construct and is testable in many different forms (mocked, injected, null, etc.). Private field injection is using a backdoor in an API. Constructor injection also provides documentation and a contract.

If you are arguing "constructor injection is better for these reasons" that is fine - you find those reasons to be more of a value traded-off for the value of brevity, etc. I don't think it's nearly so certain a win, but I've not met many developers who aren't "absolutely certain" that their choice between field and constructor injection is "the right way". So take from that what you will. Personally, I find constructor injection to be a slight win, where you can do it.

Additionally, the JVM makes guarantees about language constructs like constructors. It does not do this for libraries, even those with JSR interfaces like Guice. Although, Guice is solid and we all trust it, field injection does not have the same guarantees from the JVM as constructors.

And again, I don't think field injection advocates are saying those are all illusory, I think they're saying that in their "lived experience" (so to speak) they simply don't find these advantages to actually catch very many bugs, prevent very many bugs, nor communicate intent any more intentionally or clearly than the other signals with field injection. In short, they can all agree with this statement and still not care. Because it is a trade-off between advantages, and those comparisons and valuations are subjective.

Christian.

Christian Gruber :: Google, Inc. :: Java Core Libraries :: Dependency Injection
email: [email protected] :::: mobile: +1 (646) 807-9839

--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to