<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Darin Boyd Dot Com &#187; .Net</title>
	<atom:link href="http://www.darinboyd.com/wordpress/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.darinboyd.com/wordpress</link>
	<description>This is what happens when you're interested in everything!</description>
	<lastBuildDate>Fri, 23 Jan 2009 05:41:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cleaner Configuration Code for .Net</title>
		<link>http://www.darinboyd.com/wordpress/2009/01/cleaner-configuration-code-for-net/</link>
		<comments>http://www.darinboyd.com/wordpress/2009/01/cleaner-configuration-code-for-net/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 09:13:08 +0000</pubDate>
		<dc:creator>Darin Boyd</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.darinboyd.com/wordpress/?p=88</guid>
		<description><![CDATA[Microsoft&#8217;s configuration system is very handy, but often configuration access code can get pretty nasty and result in a lot of duplication. In preparation for another article I am creating for the site, I decided to share a simple technique I use for managing my config code.
The Normal Way
Suppose that you have a few config [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft&#8217;s configuration system is very handy, but often configuration access code can get pretty nasty and result in a lot of duplication.<span id="more-88"></span> In preparation for another article I am creating for the site, I decided to share a simple technique I use for managing my config code.</p>
<h2>The Normal Way</h2>
<p>Suppose that you have a few config settings in the config file.  Our example has three settings.  Two of the settings are strings and the last one should be cast to a long.  Let us also suppose that we want to throw an exception reminding the developer that the application is not configured correctly if either of the string settings are missing.  For the long setting, we do not wish to throw an error if the setting is missing.  We would prefer to assume a default value unless the config file says otherwise.  If we don&#8217;t have a class in which to house these policies,  the code gets pretty messy.  Below is and example of a few simple calls with no wrapper.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Before<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// get the connection string for the database</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ConfigurationManager.<span style="color: #0000FF;">ConnectionStrings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CacheDBConnection&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Config setting 'CacheDBConnection' does not exist in the config file.&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #008080; font-style: italic;">// if the exception doesn’t throw, we can keep going</span>
    <span style="color: #FF0000;">string</span> _ConnectionString <span style="color: #008000;">=</span> ConfigurationManager.<span style="color: #0000FF;">ConnectionStrings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CacheDBConnection&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ConnectionString</span>;
&nbsp;
    <span style="color: #008080; font-style: italic;">// get the default size for the cache from the config file</span>
&nbsp;
    <span style="color: #FF0000;">long</span> _CacheSize <span style="color: #008000;">=</span> 0;
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ConfigurationManager.<span style="color: #0000FF;">ConnectionStrings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CacheSizeLimit&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        _CacheSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">long</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CacheSizeLimit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The above approach has many disadvantages including the following items.</p>
<ul>
<li>It is verbose</li>
<li>Making several calls like this would generate a lot of duplicate code.  This is a real problem for maintenance.</li>
<li>It is likely that many people would not put in repetitious checks for a valid configuration out of sheer laziness.</li>
<li>You have to remember the name of the config settings without any help from intellisense while you&#8217;re in the heat of coding.</li>
</ul>
<h2>A Solution to Messy Config Code</h2>
<p>The manager class I created here was built for a caching manager, so it is called <strong>ObjectCacheConfig</strong>. You can build your config class to your own specifications taking some inspiration from the class presented.  The class file may be downloaded by clicking <a title="source code" href="http://www.darinboyd.com/wordpress/wp-content/uploads/2009/01/objectcacheconfig.zip" target="_blank"></a><a href="http://www.darinboyd.com/wordpress/wp-content/uploads/2009/01/objectcacheconfig.zip">here</a>.</p>
<p>I built this class as a static class to avoid the need for an instance.  This is a fairly common pattern in .Net and I use it frequently for utility classes which need no state. The public static properties access the appropriate config file settings and perform any validity checks and casting required.   The <strong>VerifyAppSetting()</strong> helper method allows some flexibility in your policy regarding missing config settings.  <strong>VerifyConnectionString()</strong> will throw an error if the connection string does not exist.</p>
<p><strong>Note: </strong><em>In the code example below, I was unable to include my usual XML documentation comments due to formatting problems with my source code display plugin.  The XML comments are included with the actual source file.</em></p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Configuration</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> ObjectCacheCore
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ObjectCacheConfig
    <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #008080;">#region Static Properties</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> CacheDBConnection
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                VerifyConnectionString<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;CacheDBConnection&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #0600FF;">return</span> ConfigurationManager.<span style="color: #0000FF;">ConnectionStrings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CacheDBConnection&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ConnectionString</span>;
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> CacheDBTable
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                VerifyAppSetting<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;CacheDBTable&quot;</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
&nbsp;
                <span style="color: #0600FF;">return</span> ConfigurationManager.<span style="color: #0000FF;">ConnectionStrings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CacheDBTable&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ConnectionString</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">long</span> CacheSizeLimit
        <span style="color: #000000;">&#123;</span>
            get
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">long</span> _SizeLimit <span style="color: #008000;">=</span> 0;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>VerifyAppSetting<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;CacheSizeLimit&quot;</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    _SizeLimit <span style="color: #008000;">=</span> <span style="color: #FF0000;">long</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;CacheSizeLimit&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">return</span> _SizeLimit;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region Helper Methods</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> VerifyAppSetting<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> settingName, <span style="color: #FF0000;">bool</span> throwIfNull<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">bool</span> _Exists <span style="color: #008000;">=</span> ConfigurationManager.<span style="color: #0000FF;">AppSettings</span><span style="color: #000000;">&#91;</span>settingName<span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> null;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_Exists <span style="color: #008000;">&amp;&amp;</span> throwIfNull<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> _Message <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Application setting '&quot;</span> <span style="color: #008000;">+</span> settingName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' does not exist in the config file.&quot;</span>;
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ConfigurationErrorsException<span style="color: #000000;">&#40;</span>_Message<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> _Exists;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> VerifyConnectionString<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> connectionName<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">bool</span> _Exists <span style="color: #008000;">=</span> ConfigurationManager.<span style="color: #0000FF;">ConnectionStrings</span><span style="color: #000000;">&#91;</span>connectionName<span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> null;
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_Exists<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> _Message <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Connection string '&quot;</span> <span style="color: #008000;">+</span> connectionName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' does not exist in the config file.&quot;</span>;
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ConfigurationErrorsException<span style="color: #000000;">&#40;</span>_Message<span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<h2>Life After the Config Class</h2>
<p>Let&#8217;s see what our access code would look like now that the config manager class has been written.  The code below accomplishes the same result as our first code sample in the article.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> After<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// get the connection string</span>
    <span style="color: #FF0000;">string</span> _ConnectionString <span style="color: #008000;">=</span> ObjectCacheConfig.<span style="color: #0000FF;">CacheDBConnection</span>;
&nbsp;
    <span style="color: #008080; font-style: italic;">// get the configured cache size limit</span>
    <span style="color: #FF0000;">long</span> _CacheSize <span style="color: #008000;">=</span> ObjectCacheConfig.<span style="color: #0000FF;">CacheSizeLimit</span>;
&nbsp;
<span style="color: #000000;">&#125;</span></pre></div></div>

<h2>In Conclusion</h2>
<p>Using a pattern similar to the above class can really clean up your application code with regard to config access. We reduced the code size and eliminated duplicate code.  Now, while coding, Visual Studio will give you a drop down menu of your config settings with full documentation for the setting if you have your xml comments in place.    It is a simple pattern, yet it could change your code dramatically if you use a lot of config settings.</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.darinboyd.com/wordpress/2009/01/cleaner-configuration-code-for-net/feed/</wfw:commentRss>
		<slash:comments>1240</slash:comments>
		</item>
	</channel>
</rss>
