Click here to Skip to main content
12,887,625 members (30,818 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 20 Jul 2010

Enterprise Library 5 Fluent Configuration API

, 21 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use the Fluent configuration API with the Data Access Application Block.


One of the new Enterprise Library 5 improvements is a new Fluent configuration API. In this post, I'll explain the subject and then show how to use the Fluent configuration API with the Data Access Application Block.

Fluent Configuration API

There are times that we would like to configure our application at runtime without using a configuration file such as web.config or app.config. In order to achieve that, we can use the new Fluent configuration API that was shipped with Enterprise Library 5. The API can be used to configure the core, instrumentation, and all of the application blocks not including the Validation and Policy Injection application blocks. Also, if you already have an Enterprise Library configuration in your config file, you will be able to merge the configuration you created at runtime to it or update it.

Using the Fluent Configuration API

In order to use the Fluent configuration API, you need to create a ConfigurationSourceBuilder which is the main class to build a runtime configuration. Each feature in Enterprise Library, such as the application blocks for example, provides extension methods for this class which enables us to use the API in the same manner. The use of extension methods is very intuitive and easy. The ConfigurationSourceBuilder class is located in the Microsoft.Practices.EnterpriseLibrary.Common.Configuration DLL and you need to reference it. In order to use the Fluent configuration extension methods for every application block, you need to add a reference to that application block’s DLL.

DAAB Fluent Configuration API Example

Let's look at a DAAB Fluent configuration API example:

public void ConfigureDAAB()
  var configBuilder = new ConfigurationSourceBuilder();

  var configSource = new DictionaryConfigurationSource();
  EnterpriseLibraryContainer.Current =

What you see here is that I created a ConfigurationSourceBuilder instance.

Then I use the Fluent configuration API to configure it. I instructed the data configuration to be a SQL Server provider (using the ASqlDatabase method), give it a connection string, and set it to be my default database.

Then I create a DictionaryConfigurationSource which holds my DAAB configuration when I use the UpdateConfigurationWithReplace method in order to update the configurations, or if it exists, to replace it.

In the end, I set the EnterpriseLibraryContainer to be configured from that source.


Enterprise Library 5 comes with a new Fluent configuration API which enables us to configure our EnterpriseLibraryContainer in runtime.

The API is very intuitive and easy to learn. I have shown above a straightforward example of how to configure the default database for the Data Access Application Block.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...

Comments and Discussions

QuestionHow to configure and use Microsoft Enterprise Library 5 for logging Pin
Libish Varghese Jacob17-Jul-12 20:14
memberLibish Varghese Jacob17-Jul-12 20:14 
GeneralAdding a DB connection to an existing configuration [modified] Pin
ellarr27-Oct-10 9:18
memberellarr27-Oct-10 9:18 
In my system, the default datasource in app.config has the location of my settings database. There are other databases (end user configurable, not administrator only) that I need to interact with. However, these are stored in the settings database using something other than Enterprise Library XML configuration format.

Running your code replaces the default datasource with the new databases. I've done some searching online, but can't seem to find a way to configure datasources without waxing the existing ones.

How do I add database configurations to the Enterprise Library without removing the configurations already in there?

--- Updated to answer my own question after some digging ---

<small>// Get an instance of a builder and the config so we can incrementally add datasources<br />
                            ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();<br />
                            IDataConfiguration builderConfig = builder.ConfigureData();<br />
<br />
                            // Load the configuration from our application config file<br />
                            IConfigurationSource configSource = ConfigurationSourceFactory.Create();<br />
<br />
                            // Retrieve the default datasource name<br />
                            DatabaseSettings dataConfigurationSection = (DatabaseSettings)configSource.GetSection("dataConfiguration");<br />
                            string defaultDatabaseName = dataConfigurationSection.DefaultDatabase;<br />
<br />
                            // Add the configurations from our application config file to the builder<br />
                            ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)configSource.GetSection("connectionStrings");<br />
                            foreach (ConnectionStringSettings connectionStringsSettings in connectionStringsSection.ConnectionStrings)<br />
                            {<br />
                                IDatabaseConfigurationProperties props = builderConfig.ForDatabaseNamed(connectionStringsSettings.Name);<br />
                                switch (connectionStringsSettings.ProviderName)<br />
                                {<br />
                                    case "System.Data.SqlClient":<br />
                                        props = props.ThatIs.ASqlDatabase().WithConnectionString(connectionStringsSettings.ConnectionString);<br />
                                        if (connectionStringsSettings.Name.Equals(defaultDatabaseName))<br />
                                        {<br />
                                            props.AsDefault();<br />
                                        }<br />
                                        break;<br />
                                    default:<br />
                                        throw new ApplicationException(String.Format("Unsupported provider name:{0}", connectionStringsSettings.ProviderName));<br />
                                }<br />
                            }<br />
<br />
                            // Add our database connections to the builder<br />
                            foreach (KeyValuePair<string, string> dbConnection in m_dbConnections)<br />
                            {<br />
                                builderConfig.ForDatabaseNamed(dbConnection.Key).ThatIs.ASqlDatabase().WithConnectionString(dbConnection.Value);<br />
                            }<br />
<br />
                            // Create a new configuration source, update it with the builder and make it the current configuration<br />
                            DictionaryConfigurationSource newConfigSource = new DictionaryConfigurationSource();<br />
                            builder.UpdateConfigurationWithReplace(newConfigSource);<br />
                            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(newConfigSource);</small>

modified on Wednesday, October 27, 2010 8:58 PM

GeneralRe: Adding a DB connection to an existing configuration Pin
Gil Fink27-Oct-10 21:46
memberGil Fink27-Oct-10 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 22 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid