Apache Tapestry and DirectLink, IoC and DI - Inversion of Control and Dependency Injection (Page 4 of 4 )
Inversion of Control (IoC) and Dependency Injection (DI) are some of the most popular concepts in software development these days, at least in the world of Java. However when I decided to find out what these words mean, I found that most of the available definitions are incomprehensible, while the concepts themselves are quite simple. So let me make yet another attempt to explain what IoC and DI actually mean.
Imagine you are staying in a five star hotel. Are you going to cook your breakfast or wash your shirts there? Probably not. You will trust the hotel to do all the low level work while you are doing something much more important like sunbathing or visiting local attractions. That's quite a natural thing to do, but from the computer science point of view this approach is labeled "Inversion of Control."
In general, IoC means that you don't want to deal with some low level basic functionality in your code (you don't want to control that stuff yourself as you have more interesting things to do), so you leave it to a container (like that hotel) in which your code runs.
We have already dealt with IoC in Tapestry when we used just an abstract getter to tell the framework that we need to have a fully configured and properly maintained page property. Tapestry itself can be considered a container, although it would be more proper to say that Tapestry 4 is built on top of an IoC container named HiveMind.
HiveMind is a separate Apache project, also created by Howard Lewis Ship, the creator of Tapestry. We are going to deal with HiveMind configuration in the upcoming articles, but for now it is enough just to know that it exists. Arguably, HiveMind is more sophisticated than the much better known IoC container of Spring framework. By the way, we can easily use Spring beans in a Tapestry application. Strangely enough, it might be easier to get a Spring bean reference into Tapestry than into Spring itself! But I digress; this is a topic for a later article.
Now what about Dependency Injection? It is actually a variety of IoC, a more specialized version of it. Back in your five star hotel, you were very thirsty and consumed all the contents of your mini-bar. Does this mean that you will die from thirst the next evening? Not at all, the hotel (your container) will put (inject!) more drinks into your mini-bar. Similar to this, when we need some resource (dependency) in our Tapestry application, as we needed a reference to the Details page recently, we only provide a way to inject that dependency for us (perhaps using an annotation and an abstract getter), so that it is ready to use when we need it.
I hope the only question that still bothers you now is why such weird words are used: injection, inversion... See, some software developers like to think of themselves as not mere programmers but computer scientists. And what typically distinguishes any scientist from ordinary people? It is perhaps their tendency to use incomprehensible weird terms to label simple things. So here we are.
What comes next
We are going to create a page for adding a new celebrity. But before doing this we'll want to make a DataSource stored in the application's memory and easily accessible for all pages. In other words, we are going to make it an Application State Object (ASO).
See you in the next article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |