DatabaseConfigurationView Class in Enterprise Library Data Access Application Block - Read Database Connection Strings and Configuration

DatabaseConfigurationView Class in Enterprise Library Data Access Application Block - Read Database Connection Strings and Configuration

by David Hayden ( Florida ASP.NET Developer ), Filed: Enterprise Library 3.0

 

I have been regretfully sick since Thursday of last week and it only seems to have gotten worse. I am all pumped up on Antibiotics, Cough Medicine, and what have ya. As of today I have completely lost my voice. But, I don't need to talk and be 100% coherent to blog :)

 

DatabaseConfigurationView Class

I mentioned the DatabaseConfigurationView Class on the MSDN Message Boards today as a way to get various Enteprise Library DAAB Settings and ConnectionString information as a read-only view. It helps you read the Data Access Application Block Settings, Connection Strings, and Provider Mappings in your configuration file / source, but you can't modify it and save it back to configuration. Hence the suffix View at the end of the name of the DatabaseConfigurationView Class.

It is quite handy though for things like getting the Default Connection String Name used by the DAAB:

 

DatabaseConfigurationView view = new
DatabaseConfigurationView(new SystemConfigurationSource()); string defaultConnectionStringName = view.DefaultName;

 

You can go further and get the actual Connection String and Provider Name for the Default Connection String:

 

ConnectionStringSettings settings =
view.GetConnectionStringSettings(defaultDatabaseName); string connectionString = settings.ConnectionString; string providerName = settings.ProviderName;

 

How about we take this to the next level and use Reflection to create the Database Class ourselves, removing ObjectBuilder completely from the picture:

 

DbProviderMapping mapping =
view.GetProviderMapping(defaultConnectionStringName, providerName); ConstructorInfo ctor =
Type.GetType(mapping.DatabaseTypeName).
GetConstructor(
new Type[] {typeof (string)}); Database db = (Database)ctor.Invoke
(
new object[] { connectionString });

 

I am assuming a little bit of knowledge about the constructor I want to invoke to create the class, but I am sure we could come up with a solution to remedy that if we tried. The point is that the DatabaseConfigurationView Class is pretty helpful and has a lot of good information that you could use during runtime in your applications that use the Enterprise Library Data Access Application Block.

 

Conclusion

DatabaseConfigurationView is a pretty handy class. Don't forget a similar tutorial that looks at the DatabaseSettings Class in Enterprise Library. I will be talking about similar tips and tricks as well as new features in Enterprise Library 3.0 at the Orlando Code Camp 2007 on March 24th. Stop by and say hi!

 

I hope the tutorial helps. There are more Enterprise Library 2.0 Tutorials and Enterprise Library 3.0 Tutorials as well. Many apply to both versions.

David Hayden ( Microsoft MVP C# ), Filed: Enterprise Library 3.0

 

posted on Tuesday, March 13, 2007 8:46 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices