On dependency injection

The dangers of attribute / setter based injections
June 13, 2016 by Michael

Today is one of those days where I either react to short tempered or don’t get my point across:

This is what I’m experiencing those days, with some projects at work and also with Daily Fratze those days.

In younger (more current) projects I took really great care to avoid setter or attribute based dependency injection. You can do completely without – at least in a Spring based environment, where you can use an injectable entity manager as well.

There are enough blog posts out there that emphasize the advantages for testing, having only “complete” and ready to use instances in you hands and so on, but the above point is one of my personal main advantages.

When I first came across a real dependency injection framework nearly a decade ago I was like “wow, this is awesome!” and had suddenly @Autowired (or @Inject) at nearly every attribute and managed to create “god classes” in no time. At the beginning I still generated getters and setters as well… Not being aware, that the fields would be set via reflection anyway (removing them showed one flaw: How to test stuff without firing up the whole thing?).

Anyway, the “god classes”: Big piles of mud having a dozen collaborators or more and doing “everything”.

I’m not sure how that looks today with junior developers… Do they learn that stuff in university? Or is this temptation still real?

If you use constructor based injection you immediately get two advantages:

  • The pain of having a constructor with a dozen parameters, knowing that your thing *will* have too much responsibility.
  • A slap by your DI framework if you have circular dependencies.

Why use yet another tool in your build process to point out your obvious code smells when start up fails? If you really have to have that circle, there are ways (for example having a setter for that one dependency but than the “problem” is obvious and at last or framework dependent solutions).

Last but not least: You get dependency injection done right in my eyes, which is: no obvious need for a DI framework. You can construct and use your objects “by hand”, if you want to. That is especially true for the latest Spring Framework release 4.3, you can even omit the @Autowired on a unique, non default constructor.

The current problem I fixed today was like this: I have to create to entities that might depend on each other. For both creations I used a separate service and both services where directly accessing an entity manager and both where depending on each other (the one using the other for creating an instance, the other using the first for accessing stuff from the other entity). By introducing a data access layer in form of a Spring Data repository both could easily be decoupled. By further taking care that if a relationship between the two instances of the entities exist, the entities can only be instantiated completely (that is, fully populated, valid and ready to be persisted), I solved the circular dependency, got rid of an unnecessary BeanValidator in a service (I had to use a command object to call the other one) and remove somewhat along 40 lines of code.

No comments yet

Post a Comment

Your email is never published. We need your name and email address only for verifying a legitimate comment. For more information, a copy of your saved data or a request to delete any data under this address, please send a short notice to michael@simons.ac from the address you used to comment on this entry.
By entering and submitting a comment, wether with or without name or email address, you'll agree that all data you have entered including your IP address will be checked and stored for a limited time by Automattic Inc., 60 29th Street #343, San Francisco, CA 94110-4929, USA. only for the purpose of avoiding spam. You can deny further storage of your data by sending an email to support@wordpress.com, with subject “Deletion of Data stored by Akismet”.
Required fields are marked *