• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dependency Injection: why?

 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody. Lately I have been playing with JavaEE and Spring on Netbeans/Glassfish. Some time ago I also played with Roo and Grails which do some magic based on AOP and DI.

At first I thought it was cool... then I rapidly realized that it is a real nightmare, at least to me! I use Java because it is a static language and the main advantage is that many things can be caught at compile time instead of runtime. With dependency injection it not only happens that errors arise at runtime, they are also hard to debug! I get stacks of 5 to 8 exceptions each thrown by the other.

So I was wondering: am I missing something on DI? Is it really so cool to use instead of just having a simple factory, for example?

Let's take this:



Why couldn't it be simply like this:



Why do we have to spend time configuring XML files, learning all the syntax and the properties, getting all the errors at runtime? Is there a real advantage to outsource such configuration to XML, rather than doing it inside the factory method?

Also:



Isn't that deadly dangerous? There is no guarantee that such a bean exists!

I really don't understand why DI/IoC is used everywhere nowadays. A proper factory pattern would be a much better solution in my opinion. And by proper, I mean that errors arise at compile time:



Or am I missing something?
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this is an actual debate on multiple forums which doesn't only include DI but also dynamic languages vs static languages.

Since this would be a real long debate, I'd prefer to share some good literature about it and let you conclude by yourself what's best for the solution/system you'd be implementing.

"DIP in the Wild

The Dependency Inversion Principle (DIP) has been around since the early '90s, even so it seems easy to forget in the middle of solving a problem. After a few definitions, I'll present a number of applications of the DIP I've personally used on real projects so you'll have some examples from which to form your own conclusions."

- http://martinfowler.com/articles/dipInTheWild.html
- http://www.martinfowler.com/articles/injection.html

Cheers,
 
Author
Posts: 5
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are certainly right that it is a lot easier to diagnose problems when you can catch them at compile time rather than run time.

With the Java EE platform a great deal of work involving resources has always been done at deployment time rather than compile time. Historically this involved configuring XML files (deployment descriptors). Annotations have substantially reduced the need for XML files -- unless you need to override an annotation, you can often do without a web.xml or ejb-jar.xml file entirely.

Dependency injection allows you to put off even more until deployment time -- you can wait until deployment time, for example, to specify which implementation of an interface to use.

You are right that this increased flexibility can result in a bunch of runtime errors at deployment time if a needed class isn't reachable in your application. Making your source code simpler does hide a lot of underlying complexity. I must admit that I still have trouble wrapping my mind around some aspects of Contexts and Dependency Injection for Java EE (CDI), even though I wrote those chapters (with substantial help from the spec leads and implementers).

Thanks to William for putting the discussion in a wider context, too.
 
Martin Bechtle
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank for both replies.

Kim Haase wrote:
Dependency injection allows you to put off even more until deployment time -- you can wait until deployment time, for example, to specify which implementation of an interface to use.



Well, yes... I could imagine that with dependency injection I could have in my app a IDatabaseManager interface and then, depending on the deployment environment, decide wether to configure the app (via xml) to use a MySqlDatabaseManager implementation or a MongoDbDatabaseManager implementation, at deploy time instead of compile time.

But that's also true with Factory...



And then have a config.properties file with the same kind of configuration you would do in beans.xml (and by the way, much more concise and controllable)

So I still see no real difference in term of "features" between DI and Factory for server side code execution. *Maybe* it could be handy for client-server applications, where a client must execute remote code on the server. Still, I would not use DI on a web-app framework.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic