(Annotated) Field injection vs Constructor injection - royvanrijn


« Setter vs Constructor injection

Bug Fix Day! »

Written by Roy van Rijn (royvanrijn.com) on Sep 21, 2010 11:35:41 : 5 COMMENTS

(Annotated) Field injection vs Constructor injection A couple of people replied to my last article about constructor vs setter injection that they prefer a third option, field injection. This is a slight variant of setter injection in which we magically let the setter dissapear. So, another blogpost here! Let me first show what field injection looks like: public class Apple { @Inject private Orange orange; }

This is an example from the PicoContainer website, but Spring and Google Guice can do this too. So, you ask, what is wrong with this?

Testability I want the classes I write to be testable. Every class needs a test, and every class needs to be tested without the help of any other dependency. For every dependency in my class I want to be able to use a stub/mock implementation. With constructor injection this is easy, just do new SomeClass(…). But this just can’t be done with field injection..! How do you test these classes? You have no way to construct the objects without bytecode-magic. You cannot create an instance of the above Apple class with its dependencies without using the DI-framework. And the last thing I want is the DI-framework in my unit tests. It makes the test slow, and as we all know, tests need to run as fast as possible to be effective.

Final is still tricky When using field injection your fields can be made private, but you can’t make them final (!!). This is a common mistake, but look at the Google Guice wiki: http://code.google.com/p/google-guice/wiki/Injections. Note the warning: “Avoid using field injection with final fields, which has weak semantics.”

Code smell is wanted And the final thing I don’t like about field injection… it looks good. This is a bad thing! I tried to explain this in the previous blogpost, but failed I guess. When using constructor injection you’ll notice when it gets ugly, you’ll see that long ugly constructor… and you’ll refactor the class. When using field injection this warning is forgotten. Large constructor equals bad design. Don’t fix this by changing to setter or field injection, fix the underlying problem and improve your class granularity. The fact that is looks bad with more then 2 or 3 arguments is actually a big plus!

Annotations Yes, annotations bind you to the framework, and they are evil. But so is XML. I have to agree on one thing, annotations (javax.inject) are a good thing. But still use constructor injection with these annotations please!


(Annotated) Field injection vs Constructor injection - royvanrijn

« Setter vs Constructor injection Bug Fix Day! » Written by Roy van Rijn (royvanrijn.com) on Sep 21, 2010 11:35:41 : 5 COMMENTS (Annotated) Field i...

97KB Sizes 3 Downloads 8 Views

Recommend Documents

Constructor injection in Scala | subtype
Oct 6, 2011 - Scala allows to perform implicit constructor injection in quite elegant manner. This is should not be noth

SoftwareMill blog: Reader & Constructor-based Dependency Injection
Dec 12, 2016 - Just a side note; when talking about dependency injection, I mean its simple, constructor-based form, as

Secondary Injection Testing vs. Primary Injection Testing for Circuit
Secondary injection testing: Can be done on Electronic breakers only; Quick; Tests the trip unit only; Easier; Takes les

Injection Materials, Chemical Grouts, & Specialty Injection Resins
For the civil market this includes polyurethane grouting products (also called PU grouts) and acrylate resins for leak r

lethal injection process
PURPOSE: To establish the procedures for the execution by lethal injection of inmates sentenced to death. DEFINITIONS: .

Secondary air injection - Wikipedia
Secondary air injection is a vehicle emissions control strategy introduced in 1966, wherein fresh air is injected into t

injection molding machine for metal injection molding (mim) - IIT Delhi
May 20, 2006 - This is to certify that the report entitled "Design and Development of Micro-. Injection Molding Machine

Amphotericin-B Injection - NVBDCP
Tests: pH (2.4.24).7.2 to 8.0 determined in a solution containing 10 mg per ml of Amphotericin B. Bacterial Endotoxins (

Rubber Injection Moulding Machine
Between tie bars: 480 x 480mm; Min/max mould height: 20/500mm; Hydraulic closing; MIR K3000 barrel heating; vacuum pump.

Home injection molding | Hackaday
Aug 12, 2009 - While out of the average hacker's production capabilities, his injection molding machine is amazing to be

Prince Fox | Dogman (2018) | WEBrip Bright Lights: Starring Carrie Fisher