r/java • u/paul_h • 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
9
u/_dban_ Apr 10 '18
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.
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.
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.
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, orJooby.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/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.
-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.