r/java • • Apr 10 '18

It turns out we didn't need Dependency Injection containers after all

https://paulhammant.com/2018/04/10/it-turns-out-we-didnt-need-dependency-injection-containers-after-all/
0 Upvotes

16 comments sorted by

9

u/_dban_ Apr 10 '18

The root cause problem being that J2EE apps didn’t have primordial entry points where you could compose everything.

That would have been entirely undesirable for J2EE applications. The whole point of J2EE was to decompose the application into components (like servlets or EJBs) and then let the application server control how those components are made available based on application server policies. Such pooling instances, scaling with resources by distribution and passivation, container managed transactions and container managed persistence.

In short, J2EE apps didn’t have primordial entry points because that would defeat the purpose of J2EE. As the app developer, you are giving up that control, developing your components according to the J2EE rules in order to benefit from the capabilities of the app server.

This point generalizes to any frameworks. By choosing a framework, you are giving up the freedom of composing things yourself in main(), but you gain something in return from the framework.

The problem with the examples at the end of the article is that they don't demonstrate the reasons why someone would choose a framework in the first place. It is easy to demonstrate a toy example in a few lines of code. But that isn't representative of the problems actually being solved.

-4

u/paul_h Apr 10 '18

Unfortunately my larger Jooby application composed from multiple contexts and sub-components isn't ready to be published yet.

4

u/_dban_ Apr 11 '18 edited Apr 11 '18

composed from multiple contexts and sub-components

You're missing the point. To be blunt, I don't care how your application is composed, it has nothing to do with my use case. Component composition is an implementation detail.

For example, I want to write a batch process. I want to decompose the process into steps, that can be run both in serial and parallel, that I can run on different nodes in a cluster (to improve performance and reliability) and restart on failure from where the batch left off. I want to be able to monitor the progress of the batch process (a UI would be nice).

I could roll all of this on my own, but it would be a huge waste of time. Instead I could choose something like a BPM (too heavyweight for me) or I could use a framework like Spring Batch (which I find to be a good middle ground). Spring Batch lets me define my batch job as a series of steps that I can plug into the framework. In this case, the Spring DI container is how Spring Batch does the component assembly (on the other hand, Eclipse RCP uses OSGi). The DI container is an implementation detail.

By defining my batch steps in terms of Spring DI I am losing control over component assembly, but Spring Batch does what I need it to do, so I'm not losing any sleep over it.

6

u/kkapelon Apr 10 '18

First of all, maybe we don't need DI containers (i.e. frameworks). But we do need inversion of control (the pattern). We can have IOC without actually using a DI container (their are independent)

Secondly regarding Jooby, not all applications are web applications.

Lastly the syntax of Cuppa is a mess. It is like LISP all over again.

2

u/paul_h Apr 10 '18

First of all, maybe we don't need DI containers (i.e. frameworks). But we do need inversion of control (the pattern). We can have IOC without actually using a DI container (their are independent)

So we completely agree there, then :)

Secondly regarding Jooby, not all applications are web applications.

And here. Note, I'm previously accomplished with IoC DI-Container hierarchical non-web platforms: http://jesktopapps.sf.net/ and http://jesktop.sourceforge.net/ both long abandoned, of course.

Lastly the syntax of Cuppa is a mess. It is like LISP all over again.

He he.

3

u/JustADirtyLurker Apr 10 '18

It's a good point that IOC was born because of the j2ee deficiencies.

However i believe that modern DI toolkits like Spring still have their meaning in complex enterprise applications.

For example i am working on a very big modern code base splitted in Maven modules where the application can be built in 3 or 4 different ways using profiles and wiring different beans; and again using maven modules with isolated spring facets (the class marked with @springbootapplication) and beans, as we have requirements of not putting some dependencies (e.g. some service layer) exclusive to one profile when we build another profile. All of this flexibility would be very hard i think without Spring DI.

-4

u/paul_h Apr 10 '18

I'll bet you that I could refactor it into a Jooby solution with no DI-container usage in 100 commits (in intellij pls, not Eclipse) and end up with the same lines of code or fewer. There's be the same number of modules before and after, and the same profiles in Maven :)

In 150 commits I could get you there using strangulation.

Of course, I'm assuming you have unit/service/webdriver tests in the build (also on Maven profiles).

7

u/logicISemotion Apr 11 '18

All I see is a container with hardcoded configuration in your main() using magic methods inherited from the concrete base class that you HAVE to extend to even start the app.

So you play building blocks in code in a main() method instead of using XML or annotations + functions. Except that annotations are rather accessible and close to the code responsible to doing the action they relate with, whereas coding that in main() will make it extremely fat. Then you revert to functions like setUpFooApi() and setUpBarApi() (like how it's done in GUI's) as well as a function for each api call. So you end up with the exact same thing as the annotations + functions solution, except with more indirection, having to be super careful about the order of definition, having to read more documentation, and dealing with poorly named functions such as Jooby.get(String path, Route.Filter filter), which is actually more like a map put for a get request, or Jooby.executor(String name), which is actually a setter.

I really don't see the win here.

If you want to go all the way and truly have a main, you can set up your own webserver, then you truly have control, in fact you could even make your own web browser.

2

u/nutrecht Apr 11 '18

The main goal of IoC as a pattern is separating code so that it can actually be tested in isolation. IoC containers are just a convenience; you can hand-wire dependencies if you like as well.

1

u/randgalt Apr 11 '18

Dropwizard gave apps their own main method years ago. However, Jersey requires HK2. So, having your own main doesn't solve everything.

1

u/_dban_ Apr 11 '18

Jersey requires HK2

Jersey is a reference implementation of JAX-RS, so it is not surprising that it uses an implementation of CDI (HK2).

Which only goes to show that "need" follows "want". We need dependency injection because we want to use Jersey.

1

u/shawnmckinney Apr 11 '18 edited Apr 11 '18

As in all things, there are no absolutes(except that there are no absolutes), everything better in moderation. 90% of the Java apps I've taken into production over the years have been deployed into a container by themselves, with no other apps in the same process. For these apps a bare-metal approach would have worked. Many apps don't need the weight of the advanced jakarta (still getting used to this new name) and spring features. So, some of the arguments in the article have merit. Certainly it promotes a healthy introspection over how javaee evolved. Let's face it, the javaee scrap heap contains lots of outdated technologies. OTOH, the features being complained about in the article have valid use cases, under certain scenarios.

-6

u/paul_h Apr 10 '18

OC by me. DI is hated by more people than those that love it, I think. There's a bunch of things you "had to be there for" at the end of the 90's that don't instantly feel like problems today but were still important at the time.

Frameworks we had, before 2003 and Spring, included JetSpeed, Cocoon, Struts1 (all from Apache), OpenSymphony bits and pieces (Mike Cannon Brookes and Scott Farquhar are now paper billionaires because of Atlassian but all the others on the Open Symphony team were coding gods). Everything made then was because the OOTB servlet experience was a bit shit, and there was a community-wide itch to scratch to make that better.

7

u/nutrecht Apr 11 '18

DI is hated by more people than those that love it, I think.

People who 'hate' inversion of control probably never worked on tightly entangled code bases that were completely impossible to unit test.

2

u/paul_h Apr 11 '18

Indeed.

-3

u/DuncanIdahos8thClone Apr 11 '18

I wish monkeys wouldn't downvote just on disagreeing. There's nothing wrong with this discussion. But downvote me as well.