Backdoor DI with XStream

Simple, scary; got objects, externalize them to XML, tinker with the XML, render them back, and bingo a static IoC container implementation. No getters, setters or constructors in sight.

This article Dependency Injection with XStream follows on in the vein of "cool things you can do with XStream" that I began with this blog, combining it with my other meme of the moment, IoC.

It works because XStream uses XPath to ensure that object relationships are preserved in the same Object graph. That way when objects get unmarshalled they are created with their dependencies already injected. The defined container is transparent because it uses a Proxy implementation to delegate to its contents, simple (and restrictive) but very clean.

The two main drawbacks are that there is no lifecycle and the object definition can get a bit messy. But with a bit of extra code you can get round these issues.

I would appreciate comments.

No comments: