DCSIMG
Enterprise Library 5 Fluent Configuration API - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Enterprise Library 5 Fluent Configuration API

Enterprise Library 5 Fluent Configuration API

One of the newEnterprise Library 5 Fluent Configuration API
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 in 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, provide extension methods for this class
which enable us to use the API in the same manner. The use of the
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 also.

DAAB fluent configuration API example

Lets 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 used the fluent configuration API to configure it. I instructed the
data configuration to be a SQL Server provider (using the ASqlDatabase
method), gave it a connection string and set it to be my default database.
Then I created a DictionaryConfigurationSource which hold my DAAB
configuration when I use the UpdateConfigurationWithReplace method
in order to update the configurations or if 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
enable us to configure our EnterpriseLibraryContainer in runtime.
That API is very intuitive and easy to learn. I showed a straight forward
example of how to configure the default database for the Data Access
Application Block

I hope it will help you.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 22, 2010 1:13 PM

Twitter Trackbacks for Enterprise Library 5 Fluent Configuration API" by Gil Fink #tech #dotnet [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Enterprise Library 5 Fluent Configuration API" by Gil Fink #tech #dotnet         [microsoft.co.il]        on Topsy.com

# April 22, 2010 5:53 PM

Avi Levi said:

Great news, the option to configure ent lib at runtime

is long awaited and is a good enough reason for us to upgrade to the new version.

# April 25, 2010 8:38 AM

Reader Man said:

Thanx man,

am thinking of posting a class that eases that in one place.

# May 10, 2010 1:45 AM

opinions… » Enterprise Library 5.0 Fluent Configuration API said:

Pingback from  opinions…   » Enterprise Library 5.0 Fluent Configuration API

# May 17, 2010 6:29 PM

Gil Fink on .Net said:

Building a Simple Logging Http Module with Logging Application Block I wanted to check how to use the

# May 21, 2010 10:24 PM

Wage web » Enterprise Library Configuration said:

Pingback from  Wage web » Enterprise Library Configuration

# October 31, 2010 10:24 AM

Enterprise Application Blocks 5 | AllGraphicsOnline.com said:

Pingback from  Enterprise Application Blocks 5 | AllGraphicsOnline.com

# April 11, 2011 8:59 AM