A New Type of Dependency Injection.
Matt Raible was playing with DWR and Valang, and wondering if they could work together, and it set me off on a train of thought from which I conclude that maybe there is a type of Inversion of Control that we'd not thought of before.
There is a good summary of the types of IoC at the Pico website. Martin Fowler has written on the subject and Cedric isn't sure if type 1 and 2 are worth splitting. But at the end of the day there are currently 2 useful forms of Dependency Injection. Pico prefers the former, Spring the latter:
- Constructor Injection: (i.e. the config file specifies how to call a constructor)
- Setter Injection: (i.e. the config file specifies what setters to call on a JavaBean)
Matt wanted to know if DWR could use Valang to do Ajax based validation. I've not looked at Valang in detail, but I can say that the answer is: probably. You configure DWR to remote certain classes. There are various ways to create a class for remoting, but the most powerful is the ScriptedCreator, which is used in the current Ajax validation example.
The configuration for the Ajax validation example looks a bit like this:
<create creator="script" javascript="EmailValidator">
<param name="language" value="beanshell"/>
<param name="script">
import org.apache.commons.validator.EmailValidator;
return EmailValidator.getInstance();
</param>
</create>
In short, I'm using BSF and BeanShell to configure the commons-validator component. It struck me that ScriptedCreator is a bit like an IoC container that implements "Scripted Injection". You still get the ability to put all your configuration in a single file, and keep the dependencies out of the code, but in addition you get to call whatever constructors and setters you like. You can also specify the exact order or call a custom init() method, which Setter and Constructor based injection find harder.
I can also imagine that Scripted Injection could be considerably more compact than setter or constructor based injection.
I'm sure some people will say that Scripted Injection is evil because it blurs the boundary between data and code. But I would argue that the Spring config file was already expressive enough that it could be called a programming language of sorts (may it is even Turing-complete?) and everyone knows that it is evil to write programming languages in XML. The point is not to knock Spring - but to argue that Scripted Injection could be the answer to some of the criticism of the verbosity of Spring (particularly from those RubyOnRails types).
Revised Law of Software Envelopment.
<tongue position="cheek">Some time ago the ego that is JWZ, noted that Netscape contributed to the proof of the "Law of Software Envelopment" which was that "Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can".
E-Mail is old hat. These says - we have Thunderbird and GMail. IoC is the new E-Mail. The Revised Law of Software Envelopment is that "Every program attempts to expand until it is an IoC container or it contains one. Those programs which cannot so expand are replaced by ones which can". So now at last I know that DWR is good enough to cut it for the next decade or so.
Anyway - take it or leave it, I think that scripted injection is an interesting option for any real IoC containers.
(Updated: with correction to Pico and Spring)