views:

178

answers:

8

XML seems to be the language of the day, but it's not type safe (without an external tool to detect problems) and you end up doing logic in XML. Why not just do it in the same language as the rest of the project. If it's java you could just build a config jar and put it on the classpath.

I must be missing something deep.

+2  A: 

There's nothing intrinsically wrong with doing the configuration in code, it's just that the tendency is to use XML to provide some separation.

There's a widespread belief that somehow having your configuration in XML protects you from having to rebuild after a change. The reality in my experience is that you need to repackage and redeploy the application to deliver the changed XML files (in the case of web development anyway), so you could just as easily change some Java "configuration" files instead. Yo could just drop the XML files onto the web server and refresh, but in the environment I work, audit would have a fit if we did.

The main thing that using XML configuration achieves in my opinion is forcing developers to think about dependency injection and separation of concerns. in Spring (amongst others), it also provides a convenient hook to hang your AOP proxies and suchlike. Both of these can be achieved in Java configuration, it is just less obvious where the lines are drawn and the tendency may be to reintroduce direct dependencies and spaghetti code.

For information, there is a Spring project to allow you to do the configuration in code.

The Spring Java Configuration project (JavaConfig for short) provides a type-safe, pure-Java option for configuring the Spring IoC container. While XML is a widely-used configuration approach, Spring's versatility and metadata-based internal handling of bean definitions means alternatives to XML config are easy to implement.

Rich Seller
Thanks a bunch for the Spring Project link. Was completely unaware.
Allain Lalonde
You're welcome, though it's worth noting it's not yet at 1.0 release
Rich Seller
A: 

XML is not meant to have logic, and it's far from being a programming language.

XML is used to store DATA in a way easy to understand and modify.

Has you say, it's often used to store definitions, not business logic.

Sergio
Agreed, that it should be but in spring it does far more than that. It's quite possible to conditionally inject beans and so logic is in the XML file.
Allain Lalonde
+8  A: 

The main downside to configuration DI in code is that you force a recompilation in order to change your configuration. By using external files, reconfiguring becomes a runtime change. XML files also provide extra separation between your code and configuration, which many people value highly.

This can make it easier for testing, maintainability, updating on remote systems, etc. However, with many languages, you can use dynamic loading of the code in question and avoid some of the downsides, in which case the advantages diminish.

Reed Copsey
I find that a cop out argument. 99% of the time, if you want to change your DI configuration, it is a change that goes deep enough that you need a recompile anyways. and as long as you package your config classes separately, there is very little difference between editing an xml file, and replacing config-test with config-production. The benefits on the other hand is dealing with a language that is actually readable, and one that we have boatloads of highly evolved tools to work with.
Matt Briggs
(don't mean to come off quite as "attacky" as that sounded, I voted you up because that is why xml is used for this. I just really don't agree with the view)
Matt Briggs
Heh, I don't necessarily always agree with it either. In my current project, we do a lot of DI configuration (plus ORM configuration) in code vs. in XML, because it makes sense for us. However, this is a frequently touted argument, and does have it's place, which is why I mention it. Personally, I prefer config in code since you get compile time checking instead of having to rely on runtime checks of your configuration files, but that's a personal preference, and not directly an answer to the question.
Reed Copsey
I don't think that's a cop out at all. I've built quite a few enterprise-level systems and having separate config files saves many headaches, especially when you need to implement in different environments (i.e. DEV, QA, UAT, PROD). If you're building an app for yourself or small installation than perhaps your point could be true. But for commercial apps, having your config in XML is critical.
Eric
What is the difference between having separate config files, and having separate DI config packages? There is a difference between app configuration and DI configuration, app configuration should be human readable, DI configuration does not need to be. App configuration may be different on every machine, DI configuration should have max 3 or 4 specific setups.
Matt Briggs
to quote fowler "I often think that people are over-eager to define configuration files. Often a programming language makes a straightforward and powerful configuration mechanism. Modern languages can easily compile small assemblers that can be used to assemble plugins for larger systems. If compilation is a pain, then there are scripting languages that can work well also."
Matt Briggs
"It's often said that configuration files shouldn't use a programing language because they need to be edited by non-programmers. But how often is this the case? Do people really expect non-programmers to alter the transaction isolation levels of a complex server-side application? Non-language configuration files work well only to the extent they are simple. If they become complex then it's time to think about using a proper programming language."
Matt Briggs
+4  A: 

Martin Fowler covered this decision pretty well here:

http://martinfowler.com/articles/injection.html

Avoiding the urge to plagiarize... just go read the section "Code or configuration files".

richardtallent
A: 

Its bad because it makes testing harder.

If you're writing code and using methods like getApplicationContext() to obtain the dependencies, you're throwing away some of the benefits of dependency injection.

When your objects and services don't need to know how to create or acquire the resources on which they depend, they're more loosely coupled to those dependencies.

Loose coupling means easier unit testing. Its hard to get something into a junit if you need to instantiate all its dependencies. When a class omits assumptions about its dependencies, its easy to use mock objects in place of real ones for the purpose of testing.

Also, if you can resist the urge to use getApplicationContext() and other code based DI techniques, then you can (sometimes) rely on spring autowiring which means means even less configuration work. Configuration work whether in code or in XML is tedious, right?

nont
If that's how you'd implement it, then you're not doing dependency injection. Even if you're using xml for configuration, you still need to build your Bean Factory.
Allain Lalonde
Indeed, I'm arguing against using getApplicationContext().
nont
A: 

You mentioned Spring in a comment to your question, so that suggests you may be interested in the fact that Spring 3 lets you express your application contexts in Java rather XML.

It's a bit of a brain-bender, but the definition of your beans, and their inter-dependencies, can be done in Java. It still keeps a clean separation between configuration and logic, but the line becomes that bit more blurred.

skaffman
A: 

XML is mostly a data (noun) format. Code is mostly a processing (verb) format. From the design perspective, it makes sense to have your configuration in XML if it's mostly nouns (addresses, value settings, etc) and code if it's mostly verbs (processing flags, handler configurations, etc).

Mike Burton
+1  A: 

In my experience, close communication between the development team and the infrastructure team can be fostered by releasing frequently. The more you release, the more you actually know what the variability between your environments are. This also allows you to remove unnecessary configurability.

A corollary to conway's law applies here - your config files will resemble the variety of environments your app is deployed to (planned or actual).

When I have a team deploying internal applications, I tend to drive towards config in code for all architectural concerns (connection pools, etc), and config in files for all environmental config (usernames, connection strings, ip addresses). If there different architectural concerns across different environments, then I'll encapsulate those into one class, and make that classname part of the config files - e.g.

container.config=FastInMemoryConfigurationForTesting container.config=ProductionSizedConfiguration

Each one of these will use some common configuration, but will override/replace those parts of the architecture that need replacing.

This is not always appropriate however. There are several things that will affect your choice:

1) how long it takes after releasing a new drop before it is deployed successfully in each production environment and you receive feedback on that environment (cycle time) 2) The variability in deployed environments 3) The accuracy of feedback garnered from the production environments.

So, when you have a customer who distributes your app to their dev teams for deployment, you are going to have to make your app much more configurable than if you push it live yourself. You could still rely on config in code, but that requires the target audience to understand your code. If you use a common configuration approach (e.g. Spring), you make it easier for the end users to adapt and workaround issues in their production.

But a rubric is: configurability is a substitute for communication.

Nick Drew