Pros and Cons of Fluent and XML Configuration

655 views Asked by At

If you we're about to start a new project with some third party libraries (logging, orm, di, whatever), would you prefer to configure all this libraries via programming with fluent interfaces or with XML files?

Would you use a library that only supports one of these options (Fluent or XML)? Or do you prefer libraries that gives you the possibility to choose between a variety of configuration strategies.

For those who like code, take this as a hypothetical example (written in C#).

This is Fluent Configuration:

LogConfiguration.ConfigureStorage()
   .ForDatabase("CommonDB")
     .AsMsSqlDatabase()
     .WithConnectionString("server=(local); database=Northwind; Integrated Security=true;")
     .AsDefault();

This is XML Configuration:

<logging>
    <database name="CommonDB" provider="MSSQL" connString="server=(local); database=Northwind; Integrated Security=true;" default="true" />
</logging>

Finally, what are the Pros and Cons of Fluent and XML Configuration?

Until now, we've come to this:

Fluent Configuration in Code

Pros

  • Strongly typed evaluated by compiler
  • Conditional configuration

Cons

  • Unabled reconfigured after build

XML Configuration

Pros

  • Ability to easily change after deploy

Cons

  • XML is verbose
  • More vulnerable to typing mistake
3

There are 3 answers

0
jenson-button-event On BEST ANSWER

I tend to use xml for attributes I might want to change post-build (such as connection strings, loggers)

I prefer strongly typed (compiled) fluent configuration in code for things such as NHibernate mappings that only change during development.

4
soltysh On

If I understood you correctly by fluent you mean in code? If so, then definitely I'd choose XML. If by fluent you meant pure text files, then still I'd choose XML. Pros:

  • readability
  • self documenting tags (of course if you create those ;) )

Cons:

  • file length, but I wouldn't bother that

Currently all my projects are XML based.

0
Andriy Kryvtsun On

I would say that XML configs may be less verbose and even simpler than Java configs.

Look on my comparison of the same config on XML and on Java DSL in Spring Integrations's wireTap config

Also XML configs may be more familiar to less experienced programmers. So efforts for maintain such configs in the future may be less.