• 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

Inversion of Control

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:I've avoided Spring so far; IOC is one of those topics that makes my head hurt.


You are missing out on such a good thing. In what way(s) does it make your head hurt? I have found that it helps me write more testable, more loosely coupled code. The 180 degree turnaround in mindset is very liberating. I compare it to tenkan in my Aikido practice: it opens up your horizons to a vast number of possibilities.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I split this into a new topic. I've only done some cursory reading on IOC, but I don't understand how it's any different than just passing parameters. Where is the "inversion" part? It sounds like you are very familiar with it, so maybe you can provide some examples that will help me understand. Or recommend some good introductory reading.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:Where is the "inversion" part? It sounds like you are very familiar with it, so maybe you can provide some examples that will help me understand. Or recommend some good introductory reading.


Good question. In fact, Martin Fowler had the same issue with the name and suggested the term Dependency Injection instead.
 
Saloon Keeper
Posts: 27797
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IoC is the opposite of the Service Locator design pattern.

In a Service Locator, the logic must "reach out and pull in" connections to other components. That means that you either hard-code class references or use specialized location services (such as Factory classes) in order to find what you want.

IoC, on the other hand is "Good Things Come to those who Wait" or something like that. Instead of having specialized code inside your classes finding the external services they need, ian external framework such as Spring or JavaServer Faces (or both!) locates the components and injects them into the classes that need them.

If you are familiar with the Struts framework, you may recall that most Struts components were either required to implement an interface or extend a subclass. Thus, the only place that you could use the code in question was within the Struts system.

When JSF was designed, they decided that this had not been a good idea. They wanted to be able to use POJOs instead of specialized components. So JSF uses IoC to wire together beans instead of loading them down with framework-specific logic. Which is why I constantly admonish people in the JSF form that the more JSF-specific code they create, the more likely it is that they're doing it wrong.

POJOs offer a number of advantages. Because they're not colored by contexts, they can be re-used in other applications. They can be unit-tested without having to crank up an entire framework - I can unit-test most of my JSF logic functionality without running a webserver or JSF itself at all. And you can use plug-compatible components. Again, for testing purposes, I do things like wire in mock frameworks for things like database and email services. Since the components are injected rather than hard-coded, that means that once tested, I can use the same base component in the live application simply by changing the wiring so that instead of test/mock components being attached, the live functions are wired in. No source code changes (and their possible introduction of bugs).

The difference between IoC and passing parameters is that when you pass parameters, some other component is locating you and requesting a service of you. IoC injection injects an entire object into your component and you can then invoke whatever methods on it that you want to, as many times and ways as you like.

Incidentally, in addition to Spring and JSF, if I'm not mistaken there's another very popular framework using IoC and that's OSGi. Which is used, among other things as the framework for the Eclipse IDE application and its plugins.

Then there are specialized systems like the Apache Digester, which reads XML and constructs objects, injecting them into each other according to the rules that were defined. The Tomcat web application server is an application that's built off of that. More recent equivalents have also been done for other structured serialized formats such as JSON and YAML.

And, of course, JPA is basically doing the same thing when returning complex datamodel object trees.
 
Saloon Keeper
Posts: 15523
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inversion of Control usually implies some framework is in charge of injecting the dependencies for you. This is used a lot in web application containers, which can inject request information or application context into your controllers.

An example is JAX-RS, where you can write RESTful controllers like so:

When you browse to http://my.web.app/users/ it will display a list of users by calling the index() method. Before index() is called though, the UserRepository will be injected into the instance field by the web application container. Likewise, when you post a form to users/, it will inject data from the html form into the createUser() method parameters.
 
reply
    Bookmark Topic Watch Topic
  • New Topic