views:

204

answers:

10

I'm trying to get into IOC containers, and I notice a large number of them are using xml configuration. Can anyone enlighten me as to why many new technologies are moving towards xml config/programming models (WCF, WPF, Spring.NET, Unity, Windsor)? It seems that xml is a poor choice for specifying complex settings and it would be better to do it in code where things are typesafe and we have intellisense. I know that some may find this argumentative, but I really am curious as to why these otherwise very cool, advanced technologies are relying on xml.

+2  A: 

Things have since changed in the Java world with the possibilities offered by the use of annotations but you should read I don't get Spring by Bob Lee, the author of Google Guice.

EDIT: As mentioned in a comment, it's unfair to cite the previous post without mentioning I was too hard on Spring.... That's now fixed. Thanks for reminding me of this.

Pascal Thivent
Don't forget his follow-up, "I Was Too Hard on Spring" (http://crazybob.org/2006/02/i-was-too-hard-on-spring.html). Having said that, I like DI frameworks that support both configuration styles.
Jeff Sternal
Absolutely true, I updated my answer. Thanks.
Pascal Thivent
+2  A: 

Because it's what everybody else is doing in the .net space. It started with the web.config and people started extending that. but I do believe most of those technologies support both configuring by XML and by code. The main difference would be the ease with which a non-coder like a system administrator responsible for deploying a application to change the configuration. If that's needed then XML is still a viable option in my opinion. I do believe the .net world is starting to trend to convention over configuration instead of putting everything in XML config files.

olle
they do support doing it in code, but most of the documentation is geared towards doing it in xml.
Steve
XML became ubiquitous well before web.config
ima
+2  A: 

XML schema can be validated making it possible to be "strongly typed". One of the main reasons it is so popular is it is pretty easy to understand and there are so many parser frameworks in pretty much any programming language you want. There is nothing stopping you from using a flat file (such as fixed width or CSV), INI files, JSON files, or even custom binary formats if you want.

XML is also popular becasue most of the larger frameworks have direct serialization methods to convert from XML content to a complex object structure native to the enviornment.

Matthew Whited
I realize the schema can be strongly typed, but I meant the values in the xml. For instance, what if I mistype the name of an interface for the IOC xml. With compilation, I would know immediately that I had made a mistake. With xml config, it would blow up at runtime, correct?
Steve
Correct. Of course if you are using visual studio you could add schema extensions to the XML editor so it would give you intellisence and type checking. I'm sure other XML editors have similar abilities.
Matthew Whited
Thanks, thats good to know.
Steve
+3  A: 

In IOC and many other "configurable" technologies.

The thing is ( was ) that XML emerged as standard for document interoperability.

So instead of having everyone creating it's own format, everyone adopted the "new" format.

It was good for a while, but eventually it became annoying, as you point out.

About specifying it in code, well, the thing is, sometimes, the code have to be recompiled for the changes to take effect while the XML being text/plain allows modification without recompilation.

New formats are emerging, but none has made such impact as XML did.

OscarRyz
+3  A: 

I moved my Unity configuration into XML just for one reason - I can change configuration without re-compilation of my code. This is very useful in some cases.

Restuta
That's the reason why I also favor XML: one doesn't need to recompile to change the behavior of a system (e.g. substitute a dependency, add an logging aspect etc.). Too bad the Java guys have things like SpringIDE but .Net still has nothing comparable. So we have to use XSDs and UnitTests. IMO IoC-Containers which are purely configurable in code and not via external configuration files (Ninject, LinFu?) are missing an important part of IoC. To my mind, Attributes are also not the way to go: instead of removing dependencies from your class you add a dependency to an IoC container.
tobsen
Agreed, but there are a lot of tasks that don't require XML configurations, in this cases I prefer to use Ninject for its coolness =)
Restuta
+3  A: 

It's like wondering why hammer has a steel head when you'd rather glue things together.

Assembling application at run-time from declarative configuration files is the goal, DI itself is just means of achieving it.

If you can code configuration in, why use IoC framework at all? Take somewhat more tightly-coupled design and save yourself a lot of pain.

ima
+2  A: 

In general, I like the fluent interfaces for IoC, as mxmissile suggested (I use Unity).

While this does mean that only a developer can change things (as Matthew Whited pointed out), how often do you want a non-developer to be able to replace one class with another in your application? In those cases, you can prepare a simple configuration dialog (backed by whatever data store you want) and have the results from that control the fluent configuration. This avoids fragility and security concerns. Because if an end-user screws up your config file, you are likely to be blamed when the application crashes.

The main use case I have for changing configurations is for unit testing. In that case I can either manually inject fakes into my class under test (which is what I usually do) or re-do the fluent configuration.

TrueWill
+1  A: 

IoC containers should be considered as render engines to deploy/configure applications non-programmatically, rather than programmatic frameworks to facilitate the IoC design pattern.

Therefore, XML is used mainly for two reasons:

  1. to explicitly visualize the semantics of the constructed applications, rather than verbalize the constructing procedures.
  2. to have the configuration code interchangeable among external heterogeneous applications that provide value-added features (such as configuration display, verification, transformation, and edit). I.e. the intention is to open up the containers through data integration rather than API integration.

Fluent API lacks the schema verification of data model and also very inflexible to be used for the intended data integration, hence, not even comparable to XML configuration.

See XML in IoC containers: A Hell or a Realm for further discussion.

Ke Jin
A: 

You can have code completion for xml. For instance, there is an eclipse plugin for Spring configuration files that among other things shows the javadoc of the property being set as tooltip, offers auto-completion for the names of classes in your classpath, marks any bean references that could not be resolved within the current file etc. pp.

Configuration files are actually a form of DSL - tailored to express application configuration. This tailoring allows to express certain things more easily. For instance, how would you ensure proper application shutdown (a component must be shut down before its depedencies) when you initialize components in Java? How would you configure an interceptor around your business service layer?

As for why they are done in XML, I guess a DSL was required, and XML had the benefit of an existing rudimentary toolchain (editors, validators, parsers, ...).

meriton
A: 

Nobody mentioned that XML can be result of an XSLT transformation, and custom DSLs can be created in such way on top of the plain IoC configuration. It's the very powerful approach.

thorn