<?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>Nizar&#039;s .NET Blog</title>
	<atom:link href="http://nizarnoorani.com/index.php/feed" rel="self" type="application/rss+xml" />
	<link>http://nizarnoorani.com</link>
	<description></description>
	<lastBuildDate>Mon, 28 Mar 2011 20:14:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Declaring MEF Parts to be Transient</title>
		<link>http://nizarnoorani.com/index.php/archives/288</link>
		<comments>http://nizarnoorani.com/index.php/archives/288#comments</comments>
		<pubDate>Fri, 04 Mar 2011 20:20:32 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[AOP]]></category>
		<category><![CDATA[exports]]></category>
		<category><![CDATA[mef]]></category>
		<category><![CDATA[non-shared]]></category>
		<category><![CDATA[non-singleton]]></category>
		<category><![CDATA[parts]]></category>
		<category><![CDATA[shared]]></category>
		<category><![CDATA[transient]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=288</guid>
		<description><![CDATA[I had very hard time locating the following information and, therefore, would like to share it with others thru my blog: MEF allows your to declare your exports as either singleton or transient. The default policy is to create all parts (aka exports) as a singleton. What this means is that the MEF container will [...]]]></description>
			<content:encoded><![CDATA[<p>I had very hard time locating the following information and, therefore, would like to share it with others thru my blog:</p>
<p>MEF allows your to declare your exports as either singleton or transient.  The default policy is to create all parts (aka exports) as a singleton.  What this means is that the MEF container will search inside the specified catalogs and providers to find your export.  Once it does, it will create an instance of it and reuse it forever.  That&#8217;s good if your exports aren&#8217;t storing any state.  But if they are, then you&#8217;ll have to override the default policy by making your exports transient.  You can do so by adding the below attribute to your class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>PartCreationPolicy<span style="color: #008000;">&#40;</span>CreationPolicy<span style="color: #008000;">.</span><span style="color: #0000FF;">NonShared</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#91;</span>Export<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyType<span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;MyRuleset&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MyExportClass 
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Hope this saves you the time that it took me to find this little nugget!</p>
<p>Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/288/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WCF client-proxies cannot consume values of type IEnumerable!</title>
		<link>http://nizarnoorani.com/index.php/archives/283</link>
		<comments>http://nizarnoorani.com/index.php/archives/283#comments</comments>
		<pubDate>Thu, 24 Feb 2011 01:31:58 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[CommunicationException]]></category>
		<category><![CDATA[existing connection was forcily closed]]></category>
		<category><![CDATA[WCF exception]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=283</guid>
		<description><![CDATA[So, I spent a few hours spinning my wheels trying to figure out why I&#8217;ve been getting the following exception: [SocketException (0x2746): An existing connection was forcibly closed by the remote host] [IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.] [WebException: The underlying connection [...]]]></description>
			<content:encoded><![CDATA[<p>So, I spent a few hours spinning my wheels trying to figure out why I&#8217;ve been getting the following exception:</p>
<pre>
[SocketException (0x2746): An existing connection was forcibly closed by
the remote host]
[IOException: Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host.]
[WebException: The underlying connection was closed: An unexpected
error occurred on a receive.]
[CommunicationException: An error occurred while receiving the HTTP
response to http://myservice.mydomain.dk/MyService.svc. This could
be due to the service endpoint binding not using the HTTP protocol.
This could also be due to an HTTP request context being aborted by
the server (possibly due to the service shutting down). See server
logs for more details.]
</pre>
<p>Of the many reasons why you might see this exception, one is that the return value of a WCF service method cannot be of type System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T>.  If it is, then you&#8217;ll see the very informative and totally relevant (yes, I am being sarcastic) exception above when you try to consume the method on the client-side!</p>
<p>The solution of course is to change the service method definition to return an array.</p>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/283/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The case for C#&#8217;s dynamic keyword</title>
		<link>http://nizarnoorani.com/index.php/archives/267</link>
		<comments>http://nizarnoorani.com/index.php/archives/267#comments</comments>
		<pubDate>Wed, 23 Feb 2011 03:59:59 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tips And Tricks]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[ClientBase]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[non-generic ClientBase]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=267</guid>
		<description><![CDATA[In the post, I share a scenario which required the use of the 'dynamic' keyword.]]></description>
			<content:encoded><![CDATA[<p>Many developers feel, myself included, that C#&#8217;s &#8216;dynamic&#8217; keyword is&#8230;well, a little too dynamic.  But, recently, I faced a challenging problem which I was only able to solve through the use of the &#8216;dynamic&#8217; keyword!  Here goes&#8230;</p>
<p>In short, I had to write a method that adds a WCF behavior to a VS-generated WCF proxy class.  What I wanted to do is something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddCommonWcfBehavior<span style="color: #008000;">&#40;</span>ICommunicationObject wcfClientProxy<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ClientBase wcfClientBase <span style="color: #008000;">=</span> wcfClientProxy <span style="color: #0600FF; font-weight: bold;">as</span> ClientBase<span style="color: #008000;">;</span>
    wcfClientBase<span style="color: #008000;">.</span><span style="color: #0000FF;">Endpoint</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> MyCommonWcfBehavior<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080; font-style: italic;">/* Note that the method is taking in an object to type 
    'System.ServiceModel.ICommunicationObject' which is the base-level 
    interface for all WCF client proxies.
*/</span></pre></div></div>

<p>However, I couldn&#8217;t do this for the simple reason that ClientBase<> is a generic class and .NET does not provide a non-generic version.  Go figure!  And no, for reasons that are too detailed to list here, I could not make AddCommonWcfBehavior() a generic method.  I wish .NET offered a non-generic ClientBase class.  But it doesn&#8217;t and thus I was stuck!  </p>
<p>It was then that I remembered reading about the dynamic keyword not too long ago.  Using the dynamic keyword, the method looks like the following (and works as expected):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddCommonWcfBehavior<span style="color: #008000;">&#40;</span>ICommunicationObject wcfClientProxy<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    dynamic wcfClientBase <span style="color: #008000;">=</span> wcfClientProxy<span style="color: #008000;">;</span>
    wcfClientBase<span style="color: #008000;">.</span><span style="color: #0000FF;">Endpoint</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> MyCommonWcfBehavior<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So the moral of the story:  Keep the dynamic keyword in mind!  In some cases, it may be your one and only savior! </p>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/267/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Strategy Pattern at work</title>
		<link>http://nizarnoorani.com/index.php/archives/239</link>
		<comments>http://nizarnoorani.com/index.php/archives/239#comments</comments>
		<pubDate>Mon, 08 Mar 2010 18:21:13 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[feature overlap]]></category>
		<category><![CDATA[ood]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[strategy design pattern]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=239</guid>
		<description><![CDATA[We recently ran into a challenging design issue at work. We are working on a web application that must support internationalization since we have customers in many different countries. The profile for each customer has different settings and configurations based upon the customer&#8217;s country. These settings/configurations overlap and differ across countries. The goal, of course, [...]]]></description>
			<content:encoded><![CDATA[<p>We recently ran into a challenging design issue at work.  We are working on a web application that must support internationalization since we have customers in many different countries.  The profile for each customer has different settings and configurations based upon the customer&#8217;s country.</p>
<p>These settings/configurations overlap and differ across countries.  The goal, of course, is to design/implement the code in a way that enables us to reuse code that is common across countries.  Below is the scenario that we are dealing with, </p>
<ul>
<li>USCustomer.FeatureA() is the same as CACustomer.FeatureA()</li>
<li>USCustomer.FeatureB() is the same as KRCustomer.FeatureB()</li>
<li>CACustomer.FeatureC() is the same as KRCustomer.FeatureC()</li>
<li>KRCustomer.FeatureA() differs from USCustomer.FeatureA()</li>
<li>CACustomer.FeatureB() differs from USCustomer.FeatureB()</li>
<li>USCustomer.FeatureC() differs from CACustomer.FeatureC()</li>
</ul>
<p>The first attempt was of course to try and solve this via sub-classing.  Let&#8217;s look at the different options that we have for sub-classing:</p>
<ol>
<li>Make CA &#038; KR sub-classes of US.  This allows us to re-use USCustomer.FeatureA() for CA and USCustomer.FeatureB() for KR.  But it doesn&#8217;t allow us to re-use CACustomer.FeatureC() for KR.
</li>
<li> Okay, no problem, you say.  We&#8217;ll just make KR a sub-class of CA.  So, CA is a sub-class of US and KR is a sub-class of CA.  But then we run into the issue that USCustomer.FeatureB() can no longer to be re-used for KRCustomer.FeatureB().
</li>
</ol>
<p>Now imagine this kind of situation spread across 15 different countries.  Ouch!  So sub-classing clearly is not the answer.  </p>
<p>It sounds like we need some sort of &#8220;strategy&#8221; that&#8217;ll let us swap/differ settings and configurations across countries without all this coupling.  Well the strategy is to apply the Strategy design pattern.  Below is the overall strategy:</p>
<ol>
<li>
Encapsulate the features:  Each feature gets its own class.  So we end up with the following classes:</p>
<pre>
Customer
CACustomer
KRCustomer
FeatureA : IFeatureA
FeatureB : IFeatureB
FeatureC : IFeatureC
FeatureA1 : IFeatureA
FeatureB1 : IFeatureB
FeatureC1 : IFeatureC
</pre>
</li>
<li>
Setup the constructor of the Customer class to take in the concrete class that is specific to the feature that they require as a parameter.  For instance,</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> 
     <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> FeatureA<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> FeatureB<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> FeatureC<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>  <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">public</span> Customer<span style="color: #008000;">&#40;</span>IFeatureA a, IFeatureB b, IFeatureC c<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      featureA <span style="color: #008000;">=</span> a<span style="color: #008000;">;</span>
      featureB <span style="color: #008000;">=</span> b<span style="color: #008000;">;</span>
      featureC <span style="color: #008000;">=</span> c<span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> DoInitialSetup<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      SetupFeatureA<span style="color: #008000;">&#40;</span>featureA<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      SetupFeatureB<span style="color: #008000;">&#40;</span>featureB<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      SetupFeatureC<span style="color: #008000;">&#40;</span>featureC<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">private</span> IFeatureA featureA<span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> IFeatureB featureB<span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> IFeatureC featureC<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CACustomer <span style="color: #008000;">:</span> Customer
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> CACustomer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> 
     <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> FeatureA<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> FeatureB1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> FeatureC1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> KRCustomer <span style="color: #008000;">:</span> Customer
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> KRCustomer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> 
     <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> FeatureA1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> FeatureB<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">new</span> FeatureC1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

</li>
</ol>
<p>And voila!  We can now swap implementations across countries and add different implementations for different countries as needed.</p>
<p>The above example was simplified to effectively demonstrate the Strategy pattern without confusing the reader.  In particular, in the above example, the customer classes directly instantiate the features that they need.  This is generally a bad idea in practice for at least two reasons: </p>
<ol>
<li>It makes it difficult to unit test the Customer class.</li>
<li>Each type of customer is still coupled to the specific feature-set that it is using.  In other words, the features cannot be changed dynamically.
</li>
</ol>
<p>Instead of directly instantiating the classes, we should be using an IOC container to handle the object instantiations and configurations.  </p>
<p>For instance, for our application, we are using an XML file to tie concrete implementations with their respective countries.  Below is an example:</p>
<p><b>DefaultLocalization.xml</b></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interface</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;IFeatureA&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FeatureA&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interface</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;IFeatureB&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FeatureB&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interface</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;IFeatureC&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FeatureC&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><b>CALocalization.xml</b></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interface</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;IFeatureB&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FeatureB1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interface</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;IFeatureC&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FeatureC1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><b>KRLocalization.xml</b></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interface</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;IFeatureA&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FeatureA1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;interface</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;IFeatureC&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FeatureC1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/interface<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
</ol>
<p>Based on the current country, the right classes get instantiated.  If we ever need change KR to use FeatureC instead of FeatureC1, all we do is make the change in the XML file and we&#8217;re done. </p>
<p>So, in conclusion, the key to the strategy pattern is encapsulation:  Remove the logic that differs from the logic that stays the same via encapsulation.  </p>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/239/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adding a UNIQUE constraint</title>
		<link>http://nizarnoorani.com/index.php/archives/232</link>
		<comments>http://nizarnoorani.com/index.php/archives/232#comments</comments>
		<pubDate>Fri, 05 Mar 2010 20:50:03 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tips And Tricks]]></category>
		<category><![CDATA[sql unique]]></category>
		<category><![CDATA[unique constraint]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=232</guid>
		<description><![CDATA[I always forget how to do this and then have to spin my wheels searching websites for it. Finally, decided to post it on my blog so I (and others) can quickly get to it: ALTER TABLE SomeTable ADD CONSTRAINT unique_key UNIQUE (ColumnOne, ColumnTwo, ...)]]></description>
			<content:encoded><![CDATA[<p>I always forget how to do this and then have to spin my wheels searching websites for it.  Finally, decided to post it on my blog so I (and others) can quickly get to it:</p>
<pre language="sql">
ALTER TABLE SomeTable
	ADD CONSTRAINT unique_key
	UNIQUE (ColumnOne, ColumnTwo, ...)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/232/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress on a GoDaddy hosted site &#8211; 500 internal server error!</title>
		<link>http://nizarnoorani.com/index.php/archives/212</link>
		<comments>http://nizarnoorani.com/index.php/archives/212#comments</comments>
		<pubDate>Sat, 20 Feb 2010 17:19:13 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[Hosting]]></category>
		<category><![CDATA[Tips And Tricks]]></category>
		<category><![CDATA[500 internal error]]></category>
		<category><![CDATA[godaddy]]></category>
		<category><![CDATA[IIS hosting]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=212</guid>
		<description><![CDATA[Is your blog site returning the infamous &#8220;500 &#8211; internal server error&#8221; more often than not? Are you using WordPress as your blog publishing platform? Are you using GoDaddy as your website hosting provider? Are you using IIS as your web server (as opposed to Linux)? If yes, continue reading to fix this issue: There [...]]]></description>
			<content:encoded><![CDATA[<div style="line-height:1.7em">
<ul>
<li>Is your blog site returning the infamous &#8220;500 &#8211; internal server error&#8221; more often than not?</li>
<li>Are you using WordPress as your blog publishing platform?</li>
<li>Are you using GoDaddy as your website hosting provider?</li>
<li>Are you using IIS as your web server (as opposed to Linux)?</li>
</ul>
<p>If yes, continue reading to fix this issue:</p>
<p>There is weird configuration issue with running WordPress with a IIS web server on GoDaddy.  Taking the following actions will resolve this issue:</p>
<ol>
<li>
Upgrade WordPress to the latest version.  Detailed instructions at:<a href="http://codex.wordpress.org/Upgrading_WordPress" target="_blank"> Upgrading WordPress</a>
</li>
<li>
Install WP-Super-Cache plugin.  <a href="http://wordpress.org/extend/plugins/wp-super-cache/" target="_blank">Here</a> is the download link for it.  In order to enable the plugin, you have to do a little tweaking.  Below are the steps you need to follow:</p>
<ul>
<li>First off, extract the &#8216;wp-super-cache&#8217; folder into your &#8216;wp-content/plugins&#8217; directory.</li>
<li>Log in to your GoDaddy account and navigate to Hosting -> Manage Account.</li>
<li>Click on the Content tab and select File Manager.</li>
<li>Here you will see all your files and folders.  Check the wp-content folder and click the Permissions icon on the top.  Uncheck Inherit and check the Read and Write checkboxes.  Don&#8217;t worry this is only temporary to get your plugin working correctly.  Save your changes.</li>
<li>Now Enable the plugin by going to your WordPress Plugins page.</li>
<li>Next you will have to enable caching.  For this, go back to your File Manager and browse to the &#8220;wp-content/cache&#8221; folder.  Change the permissions on this folder to Read/Write as well.</li>
<li>Now open up the wp-config.php page that&#8217;s in the root directory and add the following line towards the end: define(&#8216;WP_CACHE&#8217;, true)</li>
<li>Go back to the Settings page for the wp-super-cache plugin and set the status to ON.  Make sure to save your settings.  You may see a warning about apache mod_rewrite not enable or installed.  Don&#8217;t worry about this warning.  This just means that your caching will only be HALF-ON which is fine because even it being HALF-ON will fix our &#8217;500 &#8211; internal error&#8217; issue.</li>
<li>Go back to the File Manager and change the permissions on the wp-content folder to Inherit.</li>
<li>Your website should now respond faster and stop generating the 500 &#8211; internal server error issue.</li>
</ul>
</li>
<li>As an additional security measure, I would also recommend installing the <a href="http://wordpress.org/extend/plugins/wp-spamfree/" target="_blank">WP-SpamFree</a> plugin.  This is a very effective plugin that will eliminate virtually all SPAM comments.  Thus decreasing the load on your website.</li>
</ol>
<p>If that still doesn&#8217;t fix the &#8220;500 internal server error&#8221; issue, then try updating your current WordPress theme or switch to a different WordPress theme.  If that doesn&#8217;t work either, then it&#8217;s time to bite the bullet and switch to Linux! <img src='http://nizarnoorani.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</div>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/212/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Refactoring Code to enable Unit Testing</title>
		<link>http://nizarnoorani.com/index.php/archives/173</link>
		<comments>http://nizarnoorani.com/index.php/archives/173#comments</comments>
		<pubDate>Mon, 01 Feb 2010 23:01:05 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Tests]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=173</guid>
		<description><![CDATA[Imagine the following (which you&#8217;ve very likely run into in the past): You join a new company and inherit a humongous existing code base. You are told to add some new functionalities without of course breaking existing functionality. &#8220;No problem&#8221;, you say! &#8220;I&#8217;ve done this before.&#8221; You&#8217;ve got clear requirements and complete access to the [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine the following (which you&#8217;ve very likely run into in the past):</p>
<p>You join a new company and inherit a humongous existing code base.  You are told to add some new functionalities without of course breaking existing functionality.  &#8220;No problem&#8221;, you say!  &#8220;I&#8217;ve done this before.&#8221;</p>
<p>You&#8217;ve got clear requirements and complete access to the existing code base.  After some investigation, you figure out that you need to add a few methods to an existing class.  One of these methods is:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">CustomerService<span style="color: #008000;">.</span><span style="color: #0000FF;">CanPurchaseProduct</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> productId<span style="color: #008000;">&#41;</span></pre></div></div>

<p>The existing <code>CustomerService</code> class looks somewhat like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CustomerService
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> CustomerService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> customerId<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      custManager <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      cust <span style="color: #008000;">=</span> custManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomer</span><span style="color: #008000;">&#40;</span>customerId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #008000;">...</span><span style="color: #0000FF;">other</span> methods<span style="color: #008000;">...</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">private</span> CustomerManager custManager<span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> Customer cust<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>You will also be utilizing an existing class named <code>ProductService</code>. Below is a bare-bones outline of this class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProductService
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> ProductService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> productId<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      prodManager <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProductManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      prod <span style="color: #008000;">=</span> prodManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GetProduct</span><span style="color: #008000;">&#40;</span>productId<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #008000;">...</span> <span style="color: #0000FF;">other</span> methods below <span style="color: #008000;">.....</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">private</span> ProductManager prodManager<span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> Product prod<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Your requirements state the following:</p>
<p>  &#8211; Product must be in stock<br />
  &#8211; Customer must be a member IF product is for members only  </p>
<p>Being the good developer that you are, you start, of course, by writing the unit tests.  They look something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CanPurchaseProduct_InStockAndForAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> customerId <span style="color: #008000;">=</span> <span style="color: #FF0000;">123</span><span style="color: #008000;">;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> prodId <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//TODO: Mock this scenario</span>
      CustomerService svc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerService<span style="color: #008000;">&#40;</span>customerId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
     Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>svc<span style="color: #008000;">.</span><span style="color: #0000FF;">CanPurchaseProduct</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CanPurchaseProduct_NotInStock<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> customerId <span style="color: #008000;">=</span> <span style="color: #FF0000;">123</span><span style="color: #008000;">;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> prodId <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//TODO: Mock this scenario</span>
      CustomerService svc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerService<span style="color: #008000;">&#40;</span>customerId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
     Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsFalse</span><span style="color: #008000;">&#40;</span>svc<span style="color: #008000;">.</span><span style="color: #0000FF;">CanPurchaseProduct</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> CanPurchaseProduct_NotAMember<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> customerId <span style="color: #008000;">=</span> <span style="color: #FF0000;">123</span><span style="color: #008000;">;</span>
      <span style="color: #6666cc; font-weight: bold;">int</span> prodId <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">//TODO: Mock this scenario</span>
      CustomerService svc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerService<span style="color: #008000;">&#40;</span>customerId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
     Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsFalse</span><span style="color: #008000;">&#40;</span>svc<span style="color: #008000;">.</span><span style="color: #0000FF;">CanPurchaseProduct</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Your initial implementation of the <code>CanPurchaseProduct()</code> method looks like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CustomerService 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CanPurchaseProduct<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> productId<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       ProductService prodService <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProductService<span style="color: #008000;">&#40;</span>productId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>prodService<span style="color: #008000;">.</span><span style="color: #0000FF;">IsInStock</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
         <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>prodService<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMembersOnly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>custService<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMember</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
         <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If you&#8217;re familiar with unit tests, you&#8217;ll recognize an issue right away:  The way the implementation currently stands, it is impossible to mock out the different scenarios that we have coded for in our test cases.  The reason for this is tight coupling and lack of abstraction.  The <code>CustomerManager</code>, <code>ProductService</code> and <code>ProductManager</code> classes are being directly instantiated by the classes that use them &#8211; an example of tight coupling.  Furthermore, the classes do not implement an interface thus making it impossible to substitute mock versions for them  &#8211; a direct result of lack of abstraction.  So how do we fix this with minimal changes to the existing classes?  Below is one strategy:</p>
<p>1.  Account for dependencies</p>
<p>The first step is figure out the dependencies.  A dependency is any class, resource file, configuration file, etc. that your code is using either directly or indirectly.  In our case, we have the following dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">-</span> CustomerManager
<span style="color: #008000;">-</span> ProductService
<span style="color: #008000;">-</span> ProductManager <span style="color: #008000;">&#40;</span>used by ProductService<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span></pre></div></div>

<p>2.  Make the dependencies &#8220;plug-and-play&#8221;</p>
<p>Now that we know what the dependencies are and the places where they exist, our next step is to remove all the &#8220;knots&#8221; so that we can make them plug and play.  By &#8220;knots&#8221;, I refer to statements like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">   <span style="color: #008000;">-</span> custManager <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">-</span> productService <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProductService<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>  
   <span style="color: #008000;">-</span> prodManager <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ProductManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>Instead of the classes directly instantiating dependencies, the idea is to provide it to them.  This technique is know as <a href="http://msdn.microsoft.com/en-us/magazine/cc163739.aspx">Dependency Injection</a>.  There are multiple ways of doing this.  Keeping in mind that we need make a minimum number of changes to the existing code base, we&#8217;ll go with the simplest one: </p>
<pre>Modify the constructors/methods to take in the dependencies as parameters.</pre>
<p>This is illustrated below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CustomerService
<span style="color: #008000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//Instead of directly instantiating, we pass in the dependency.</span>
   <span style="color: #008080; font-style: italic;">//All new classes can now start using this one to allow for unit      </span>
   <span style="color: #008080; font-style: italic;">//testing.</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> CustomerService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> customerId,  
     CustomerManager custManager<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      cust <span style="color: #008000;">=</span> custManager<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomer</span><span style="color: #008000;">&#40;</span>customerId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//Original constructor becomes empty.  </span>
   <span style="color: #008080; font-style: italic;">//We still need to keep it around since it is probably used other</span>
   <span style="color: #008080; font-style: italic;">//existing classes</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> CustomerService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> customerId<span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> 
       <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#40;</span>customerId, <span style="color: #008000;">new</span> CustomerManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #008000;">...</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//Note that the method is now taking ProductService as a parameter</span>
   <span style="color: #008080; font-style: italic;">//instead of directly instantiating it.</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CanPurchaseProduct<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> prodId, 
        ProductService prodService<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProductService
<span style="color: #008000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//Instead of directly instantiating, we pass in the dependency.</span>
   <span style="color: #008080; font-style: italic;">//All new classes can now start using this one to allow for unit      </span>
   <span style="color: #008080; font-style: italic;">//testing.</span>
   <span style="color: #0600FF; font-weight: bold;">public</span> ProductService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> productId, ProductManager prodManager<span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
&nbsp;
   <span style="color: #008000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">public</span> ProductService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> productId<span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> 
     <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#40;</span>productId, <span style="color: #008000;">new</span> ProductManager<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Note: When it comes time to re-factor/re-design/re-architect (whatever you want to you call) your entire application, you should go with a better alternative to handle dependency injection instead of the approach shown here.   I would recommend using a tool such as <a href="http://www.springframework.net/overview.html">Spring .NET</a>.</p>
<p>3.  Abstract them away!</p>
<p>The final step then is create an interface for each of the concrete classes and replace all references to the concrete classes with their associated interfaces.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> ICustomerService
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">bool</span> CanPurchaseProduct<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> customerId, IProductService  
    prodService<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> ICustomerManager <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IProductService <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IProductManager <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CustomerService <span style="color: #008000;">:</span> ICustomerService 
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> CustomerService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> customerId, ICustomerManager<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CanPurchaseProduct<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> prodId, IProductService 
    prodService<span style="color: #008000;">&#41;</span> 
  <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> CustomerManager <span style="color: #008000;">:</span> ICustomerManager <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProductService <span style="color: #008000;">:</span> IProductService 
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ProductService<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> prodId, IProductManager<span style="color: #008000;">&#41;</span> 
  <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ProductManager <span style="color: #008000;">:</span> IProductManager <span style="color: #008000;">&#123;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now, we can finally substitute our mocks for the dependencies in our unit tests as shown below.  Note that in the test cases below I am using the <a href="http://code.google.com/p/moq/">Moq Unit Testing Framework</a> to setup the mocks but you could use any other framework that you&#8217;d like or none at all (if you wish to create the mocks manually).</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CanPurchaseProduct_InStockAndForAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">int</span> customerId <span style="color: #008000;">=</span> <span style="color: #FF0000;">123</span><span style="color: #008000;">;</span>
   <span style="color: #6666cc; font-weight: bold;">int</span> prodId <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//create mocks</span>
   Mock<span style="color: #008000;">&lt;</span>ICustomerManager<span style="color: #008000;">&gt;</span> custManager <span style="color: #008000;">=</span> 
     mockFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&lt;</span>ICustomerManager<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   Mock<span style="color: #008000;">&lt;</span>IProductService<span style="color: #008000;">&gt;</span> prodService <span style="color: #008000;">=</span> 
      mockFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&lt;</span>IProductService<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   Customer cust <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">123</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   cust<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMember</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//setup mocks</span>
   custManager<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>cm <span style="color: #008000;">=&gt;</span> cm<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomer</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">123</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span>cust<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   prodService<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>ps <span style="color: #008000;">=&gt;</span> ps<span style="color: #008000;">.</span><span style="color: #0000FF;">IsInStock</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   prodService<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>ps <span style="color: #008000;">=&gt;</span> ps<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMembersOnly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//run test</span>
   CustomerService svc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerService<span style="color: #008000;">&#40;</span>customerId, custManager<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
   Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>svc<span style="color: #008000;">.</span><span style="color: #0000FF;">CanPurchaseProduct</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, prodService<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CanPurchaseProduct_NotInStock<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">int</span> customerId <span style="color: #008000;">=</span> <span style="color: #FF0000;">123</span><span style="color: #008000;">;</span>
   <span style="color: #6666cc; font-weight: bold;">int</span> prodId <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//create mocks</span>
   Mock<span style="color: #008000;">&lt;</span>ICustomerManager<span style="color: #008000;">&gt;</span> custManager <span style="color: #008000;">=</span> 
      mockFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&lt;</span>ICustomerManager<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   Mock<span style="color: #008000;">&lt;</span>IProductService<span style="color: #008000;">&gt;</span> prodService <span style="color: #008000;">=</span> 
      mockFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&lt;</span>IProductService<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   Customer cust <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">123</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   cust<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMember</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//setup mocks</span>
   custManager<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>cm <span style="color: #008000;">=&gt;</span> cm<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomer</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">123</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span>cust<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   prodService<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>ps <span style="color: #008000;">=&gt;</span> ps<span style="color: #008000;">.</span><span style="color: #0000FF;">IsInStock</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//run test</span>
   CustomerService svc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerService<span style="color: #008000;">&#40;</span>customerId, custManager<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
   Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsFalse</span><span style="color: #008000;">&#40;</span>svc<span style="color: #008000;">.</span><span style="color: #0000FF;">CanPurchaseProduct</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, prodService<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>TestCase<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> CanPurchaseProduct_NotAMember<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">int</span> customerId <span style="color: #008000;">=</span> <span style="color: #FF0000;">123</span><span style="color: #008000;">;</span>
   <span style="color: #6666cc; font-weight: bold;">int</span> prodId <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//create mocks</span>
   Mock<span style="color: #008000;">&lt;</span>ICustomerManager<span style="color: #008000;">&gt;</span> custManager <span style="color: #008000;">=</span> 
      mockFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&lt;</span>ICustomerManager<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   Mock<span style="color: #008000;">&lt;</span>IProductService<span style="color: #008000;">&gt;</span> prodService <span style="color: #008000;">=</span> 
      mockFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&lt;</span>IProductService<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   Customer cust <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">123</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   cust<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMember</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//setup mocks</span>
   custManager<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>cm <span style="color: #008000;">=&gt;</span> cm<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomer</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">123</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span>cust<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   prodService<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>ps <span style="color: #008000;">=&gt;</span> ps<span style="color: #008000;">.</span><span style="color: #0000FF;">IsInStock</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   prodService<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>ps <span style="color: #008000;">=&gt;</span> ps<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMembersOnly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//run test</span>
   CustomerService svc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerService<span style="color: #008000;">&#40;</span>customerId, custManager<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
   Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsFalse</span><span style="color: #008000;">&#40;</span>svc<span style="color: #008000;">.</span><span style="color: #0000FF;">CanPurchaseProduct</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">1</span>, prodService<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And there you have it.  We were able to take an existing code base that had no support for unit-testing and re-factor it so that the additional methods that we added could be unit-tested.  Furthermore, we did this without requiring us to make any sweeping architectural changes to the existing code base.  </p>
<p>One could argue if the additional work is really worthwhile.  If you&#8217;re already a test-driven developer then I don&#8217;t need convince you that it is definitely worthwhile.  But if you&#8217;re not then I&#8217;ll highlight a few long-term benefits that result from the additional work:</p>
<p>- The code base is now more extensible. It can work with other implementations for ICustomerManager and IProductService.<br />
- At any given time one can run the test cases and know if anything has been broken.<br />
- Unit-testing forces us to think about the dependencies and code in a manner that leads to clean and easy-to-understand code.<br />
- The test cases serve as an up-to-date documentation on what the code does and how it is being  used.</p>
<p>I hope that the above points will encourage you to investigate the pros/cons of test driven development more fully.</p>
<p><a href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=noorani786" rel="tag" style="display:none">CodeProject</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/173/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>WCF over HTTPS</title>
		<link>http://nizarnoorani.com/index.php/archives/156</link>
		<comments>http://nizarnoorani.com/index.php/archives/156#comments</comments>
		<pubDate>Wed, 26 Aug 2009 14:54:55 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[httpTransport]]></category>
		<category><![CDATA[transport]]></category>
		<category><![CDATA[WCF over HTTPS]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=156</guid>
		<description><![CDATA[I have been working on a WCF web service that I finally deployed to our staging server which runs over HTTPS. Everything seemed fine. I was able to hit it and generate the WSDL. However, when I tried running the svcutil.exe on it, I got the following error: Error: Cannot add the transport element 'httpTransport'. [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working on a WCF web service that I finally deployed to our staging server which runs over HTTPS.  Everything seemed fine.  I was able to hit it and generate the WSDL.  However, when I tried running the <code>svcutil.exe</code> on it, I got the following error:</p>
<pre>
Error: Cannot add the transport element 'httpTransport'. Another transport eleme
nt already exists in the binding 'System.ServiceModel.Configuration.HttpTranspor
tElement, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=
b77a5c561934e089'. There can only be one transport element for each binding.
</pre>
<p>Upon investigation, I realized that when deploying a WCF web service over HTTPS, you must set the &#8216;Security&#8217; mode to &#8216;Transport.&#8217;.  Below are the steps to do this:</p>
<p>1.  Add the following inside the <system.serviceModel> section:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wsHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;webBinding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;security</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;Transport&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/security<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/wsHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>2.  Add the following attribute to the <endpoint /> element:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">bindingConfiguration=&quot;webBinding&quot;</pre></div></div>

<p>That should be all.</p>
<p>Below is a sample <system.serviceModel> section:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;services<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyWebService&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">behaviorConfiguration</span>=<span style="color: #ff0000;">&quot;ServiceBehavior&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #808080; font-style: italic;">&lt;!-- Service Endpoints --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;wsHttpBinding&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">bindingConfiguration</span>=<span style="color: #ff0000;">&quot;webBinding&quot;</span> <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;IMyWebService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/endpoint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;mex&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;mexHttpBinding&quot;</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;IMetadataExchange&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/services<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wsHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;webBinding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;security</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;Transport&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/wsHttpBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;serviceBehaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;behavior</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ServiceBehavior&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;serviceMetadata</span> <span style="color: #000066;">httpGetEnabled</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/behavior<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/serviceBehaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/156/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What the heck was I working on??</title>
		<link>http://nizarnoorani.com/index.php/archives/135</link>
		<comments>http://nizarnoorani.com/index.php/archives/135#comments</comments>
		<pubDate>Tue, 25 Aug 2009 04:16:10 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Tips And Tricks]]></category>
		<category><![CDATA[note]]></category>
		<category><![CDATA[outlook]]></category>
		<category><![CDATA[vs studio]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=135</guid>
		<description><![CDATA[Does it ever happen to you that you go to work in the morning, look at the screen and then ask yourself : What the heck was I working on?? Happens to me all the time. Maybe I was in the middle of debugging some code the previous day, or writing a unit-test or finishing [...]]]></description>
			<content:encoded><![CDATA[<p>Does it ever happen to you that you go to work in the morning, look at the screen and then ask yourself : What the heck was I working on??  Happens to me all the time.  Maybe I was in the middle of debugging some code the previous day, or writing a unit-test or finishing up the implementation of a class or going through existing code to understand what it&#8217;s doing or something else.  Given the detailed nature of software development, it&#8217;s sometimes hard to remember where you left off and from where you need to pick things up again.  </p>
<p>Below is an approach that&#8217;s worked quite well for me:<br />
<div id="attachment_144" class="wp-caption alignleft" style="width: 810px"><img src="http://nizarnoorani.com/wp-content/uploads/2009/08/untitled2.jpg" alt="Tells me where I left off" title="A note to myself" width="800" height="500" class="size-full wp-image-144" /><p class="wp-caption-text">Tells me where I left off</p></div><br />
<br/><br/><br />
I write myself a little MS Outlook note and put right in the middle of the screen. Simple, but very effective!</p>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/135/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Am I officially a &#8220;nerd&#8221;??</title>
		<link>http://nizarnoorani.com/index.php/archives/132</link>
		<comments>http://nizarnoorani.com/index.php/archives/132#comments</comments>
		<pubDate>Wed, 12 Aug 2009 03:59:06 +0000</pubDate>
		<dc:creator>Nizar</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://nizarnoorani.com/?p=132</guid>
		<description><![CDATA[okay i know this is lame.. but i am seriously getting a high from writing unit test cases.. seriously it&#8217;s such a rush to see 243 test cases all run AND pass in a matter of seconds&#8230; if i don&#8217;t stop i am afraid i&#8217;ll turn into a &#8220;testaholic&#8221;]]></description>
			<content:encoded><![CDATA[<p>okay i know this is lame.. but i am seriously getting a high from writing unit test cases.. seriously it&#8217;s such a rush to see 243 test cases all run AND pass in a matter of seconds&#8230;  if i don&#8217;t stop i am afraid i&#8217;ll turn into a &#8220;testaholic&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://nizarnoorani.com/index.php/archives/132/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  nizarnoorani.com/index.php/feed ) in 2.59845 seconds, on Feb 5th, 2012 at 7:03 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 5th, 2012 at 8:03 am UTC -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Quick Cache Is Fully Functional :-) ... A Quick Cache file was just served for (  nizarnoorani.com/index.php/feed ) in 0.00173 seconds, on Feb 5th, 2012 at 7:34 am UTC. -->
