07.11.2014 Views

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using the <strong>Test</strong> Cases 251<br />

<strong>Test</strong> Case<br />

The test case first created a dictionary configuration source and then added the<br />

connection string to the section. It then used the DatabaseProviderFactory<br />

class to create a Database instance. The test case passed the dictionary<br />

configuration source as a parameter.<br />

Problem<br />

The test case failed with the following exception:<br />

System.Configuration.ConfigurationErrorsException: The requested database instance is not<br />

defined in configuration.<br />

The application block used the DatabaseConfigurationView.GetConnectionString-<br />

Settings method to read the relevant configuration section. This class relies on the.<br />

NET Framework ConfigurationManager class, which only reads configuration files.<br />

The following is the code that caused the problem.<br />

public ConnectionStringSettings GetConnectionStringSettings(string name)<br />

{<br />

ConnectionStringSettings connectionStringSettings = ConfigurationManager.<br />

ConnectionStrings[name];<br />

…<br />

}<br />

Solution<br />

The application block should be able to read configuration information from any<br />

configuration source. If the configuration information is not defined in the specified<br />

configuration source, the application block should use the ConfigurationManager<br />

class to look in the configuration file. This is shown in the following code.<br />

public ConnectionStringSettings GetConnectionStringSettings(string name)<br />

{<br />

…<br />

ConnectionStringSettings connectionStringSettings;<br />

ConfigurationSection configSection = configurationSource.GetSection("connectionStrin<br />

gs");<br />

if ((configSection != null) && (configSection is ConnectionStringsSection))<br />

{<br />

ConnectionStringsSection connectionStringsSection = configSection as Connection-<br />

StringsSection;<br />

connectionStringSettings = connectionStringsSection.ConnectionStrings[name];<br />

}<br />

else<br />

connectionStringSettings = ConfigurationManager.ConnectionStrings[name];<br />

…<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!