Click here to Skip to main content
Licence CPOL
First Posted 20 Jul 2010
Views 9,062
Bookmarked 12 times

Enterprise Library 5 Fluent Configuration API

By | 21 Jul 2010 | Article
How to use the Fluent configuration API with the Data Access Application Block.
 
Part of The SQL Zone sponsored by
See Also

Introduction

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();
  configBuilder.ConfigureData()
         .ForDatabaseNamed("School")
           .ThatIs
           .ASqlDatabase()
           .WithConnectionString(ConnectionString)
           .AsDefault();

  var configSource = new DictionaryConfigurationSource();
  configBuilder.UpdateConfigurationWithReplace(configSource);
  EnterpriseLibraryContainer.Current =
     EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
}

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.

Summary

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.

License

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

About the Author

Gil Fink

Architect
Sela Group
Israel Israel

Member

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAdding a DB connection to an existing configuration [modified] Pinmemberellarr9:18 27 Oct '10  
GeneralRe: Adding a DB connection to an existing configuration PinmemberGil Fink21:46 27 Oct '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 22 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid