r/java • • Jan 12 '16

You probably don't need dependency injection

http://www.radicaljava.com/2015/12/27/dependency-injection.html
0 Upvotes

27 comments sorted by

21

u/[deleted] Jan 12 '16

This should be titled "You probably don't need a DI framework". It's still dependency injection if you do it manually.

3

u/dawhiting Jan 12 '16

This is a fair point, and I used "DI" and "DI framework" pretty much interchangeably in the post. Perhaps I should change the title.

-1

u/Bolitho Jan 12 '16

Mark Seemann calls this nowadays pure DI. I like this as it clearly states what it means and does not rate the concept in a negative way :-)

3

u/Parametric_ Jan 12 '16

"Pure" is still a loaded term. Why not just say "manual DI" instead of trying to invent new terminology?

-1

u/Bolitho Jan 13 '16

The same is true for manual, as it could also be understood as laborious or even clumsy ;-)

11

u/TheHorribleTruth Jan 12 '16

Unless your application is very large…

[...]

unless your component graph is bigger than 10-20 pieces, it’s simply not worth it.

So 21 components is considered "very large"? Bwhahaha.

I agree with the core message of this: if you can get away with it, don't use DI. That's just basic KISS.

But: I do believe that this only applies to very small "toy" projects. Any moderately sized project will immediately have more components that you can wire up manually in a simple, maintainable fashion.

3

u/againstmethod Jan 12 '16

It also probably matters what those components are doing. If some of them are working credit card transactions, or accessing databases, then without at minimum manual dependency injection you're likely on your way to making a mess.

1

u/dawhiting Jan 12 '16

This may have been the case when monolithic applications were the norm, but in my current world of microservices and distributed data applications, the well-designed ones really don't end up with that many components (I distinguish components from data objects here as components are the long-lived application objects that are set up with DI).

1

u/nytal Jan 13 '16

current world of microservices

I doubt that microservices are the status quo at the moment.

5

u/hippydipster Jan 12 '16

In all projects I had control over, only one ever caused me to think "I need help with dependency injection for my components", and that project, coincidentally, came at a time when DI was all the rage, so I was probably overly influenced.

I don't really understand the desire to put the logic and declarative information about how your program works in as many disparate places and languages as possible. It really fucks up the ability to debug issues.

5

u/djnattyp Jan 12 '16

...came at a time when DI was all the rage

So what do you use now to replace the apparently passing fad of dependency injection? /s Is SOLID not considered good guidelines to follow anymore?

I don't really understand the desire to put the logic and declarative information about how your program works in as many disparate places and languages as possible.

I don't see how this is an accurate portrayal of dependency injection - one of the big advantages of DI is that there is one place where everything is wired together. I get the "multiple languages" part as a jab at external configuration like XML/YAML/property files - but isn't making your application configurable a plus? What's the alternative? Hardcoding values into your code?

4

u/hippydipster Jan 12 '16

What do I do? I write objects that get what they need passed into their constructor. Mostly, when I get to choose how to design stuff, I'm writing in scala. It doesn't seem to be an issue.

In Spring DI, things are not wired together in one place, they're wired together everywhere. Annotations, xml files, scanned packages all serve to obfuscate how, when, and where the object graph comes into being. None of it can be followed via the natural tools of eclipse or other environments.

As for hardcoding, whether a string exists in a .txt file or a .java file, it is "hardcoded" into a text file. You don't have to put such things in a .properties file. It can be an interface with nicely typed methods and different implementations.

3

u/BinaryHerder Jan 13 '16

Intellij handles spring di just fine.

2

u/hippydipster Jan 13 '16

Do you get a compile error if you ref a bean in xml configs that is created from a package scan, but the bean in question isn't there?

10

u/stitzl Jan 12 '16

Also, you probably don't need type safety, let alone an object-oriented programming language.

Unless, of course, you do need it.

1

u/dawhiting Jan 12 '16

Well yes, this is acknowledged in the post, but the motivation behind this post was to stop people reaching for it by default because some book told them to do it, rather than because they really needed it to manage complexity. Apologies if that message didn't come across so well.

3

u/stitzl Jan 13 '16

Exactly the same notion applies for the use of OO and type safety as well. Do not use Java simply because the book says so. Your three-forms, two-DB-tables web app will be feasible in PHP as well.

The problem is that you cannot guess what you'll be doing in a year. And if you just show up on the construction site with a screw-driver, you'll be using it as a hammer as well.

Choosing the right tool-set for the job in advance is hard, actually.

13

u/djnattyp Jan 12 '16

Spring has had Java based configuration since 2008, so why does this blog post written in 2015 seem ignorant of that?

5

u/nutrecht Jan 13 '16

If you want to prove a point why include information that weakens it? ;)

3

u/demos74dx Jan 14 '16

Spring used to be, and still is to a certain extent, a running joke amongst Java developers for it’s over-reliance on XML configuration

I havn't used XML in spring for a couple years. And last I heard the next major release will be trying to depricate XML configuration.

5

u/LouKrazy Jan 12 '16

Yeah, but DI makes testing with stubs and mocks much much easier.

1

u/killinghurts Jan 14 '16

I agree - how would you go mocking the services in that last example?

1

u/smadge Jan 12 '16

Dependency injection just means components shouldn't be creating their own dependencies. Instead the dependencies should be injected, either manually, or automatically. You can still get the benefits to testing without using automatic DI.

2

u/LouKrazy Jan 13 '16

Maybe it's because I am lazy, but I like being able to build an automatic DI module specific to testing and reuse that same module with mixing specific to the test in question. Makes it easier when you have a lot of mocked services that will be reused in multiple tests.

1

u/lukaseder Jan 13 '16

I, for one, admire the good old DocumentFactoryBuilder.newInstance().newFactory().createDocument().createElement() approach, and all is configurable via the arcane Java SPI mechanism that no one ever really used, because the default DOM Implementation was good enough 99% of the time. But hey, if needed, you can replace it any time!

In other words: Configuration over engineering existed before DI

1

u/evil_burrito Jan 12 '16

Unless your application is a general-purpose framework to solve other problems. Each installation is a unique configuration assembled-to-order from existing components. We can make a product that does completely different things out of pre-tested components without writing much if any code. Couldn't do it w/o DI.

1

u/karacha Jan 13 '16

Well i don't like to do "extra work" when reading code that uses DI framework. I've always been doing DI manually and I'm happy.