I am not a huge fan of XML for configuration, but I also don’t want to go crazy with fluent interfaces when a bit of XML will do the trick. Hard to say when I prefer one over the other at times so it is always nice to have the option of XML or a fluent interface.
One of the nice features in Enterprise Library 5 is the option of a fluent interface to configure your application blocks. I have briefly played with the fluent interface to configure the Data Access Application Block, which is probably one of the simplest to configure. I am not qualified to judge the implementation of the fluent interface at this point so I will mainly just point out some quick findings and show some code.
ConfigurationSourceBuilder
ConfigurationSourceBuilder seems to be the main class involved with building configuration information using a fluent interface. It looks to me that each block adds an extension method to this class to fluently configure that application block. In the case of the Data Access Application Block, it adds an extension method, called ConfigureData(), to ConfigurationSourceBuilder that allows you to fluently configure the DAAB. Once you are done configuring all blocks, you essentially build the configuration source and then use it to populate your container.
That is clear as mud without an example, so here is a quick bit of code using ConfigurationSourceBuilder, ConfigureData(), DictionaryConfigurationSource, and EnterpriseLibraryContainer to set-up the IoC Container and use it to get at the default database as well as a named instance:
var builder = new ConfigurationSourceBuilder();
// Extension Method
builder.ConfigureData()
.ForDatabaseNamed("Test")
.ThatIs.ASqlDatabase()
.WithConnectionString("...")
.AsDefault()
.ForDatabaseNamed("Test2")
.ThatIs.ASqlDatabase()
.WithConnectionString("...");
// Create ConfigurationSource
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
// Set Container
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
// Get Default Database
var database = EnterpriseLibraryContainer.Current
.GetInstance<Database>();
// Get Named Instance of Database
var database2 = EnterpriseLibraryContainer.Current
.GetInstance<Database>("Test2");
Pretty straightforward really. If the EnterpriseLibraryContainer throws you a bit, read a little about IServiceLocator at EnterpriseLibraryContainer and IServiceLocator in Enterprise Library 5. This is just Microsoft Patterns & Practices attempt to let you use the IoC Container of your choice ( Autofac, Ninject, Windsor, StructureMap, etc. ) from within Enterprise Library.
Conclusion
I want to play with the fluent interface in Enterprise Library 5 a lot more, but indeed, I already love the simplicity of it compared to what we had to do in the past to configure various application blocks in code. Between this and the new GUI Configuration Editor in EntLib 5, things have certainly changed a bit with regards to configuration.
For those developers who have yet to consider fluent interfaces or don’t see the value, the XML configuration is still there and I doubt that it will be going anywhere anytime soon.
Hope this helps.

