Search code examples
configuration-managementfluent-interfacexml-configuration

Pros and Cons of Fluent and XML Configuration


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

Solution

  • 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.