<?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>iDevForFun</title>
	<atom:link href="http://www.idevforfun.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.idevforfun.com</link>
	<description>.NET Development</description>
	<lastBuildDate>Fri, 13 Jan 2012 20:04:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Code Re-use With Abstract Classes</title>
		<link>http://www.idevforfun.com/index.php/2012/01/13/code-re-use-with-abstract-classes/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=code-re-use-with-abstract-classes</link>
		<comments>http://www.idevforfun.com/index.php/2012/01/13/code-re-use-with-abstract-classes/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 15:35:22 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[abstract classes]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[object-oriented programming]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=215</guid>
		<description><![CDATA[TweetAn often overlooked feature of C# (and OO programming in general) is the use of abstract classes to maximise code re-use. Put simply, if you have code in related classes that is the same but needs to call implementation specific methods, you can probably use an abstract class to consolidate this code. Lets dive right ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton215" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2012%2F01%2F13%2Fcode-re-use-with-abstract-classes%2F&amp;via=Lawnmower_Man&amp;text=Code%20Re-use%20With%20Abstract%20Classes&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2012%2F01%2F13%2Fcode-re-use-with-abstract-classes%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>An often overlooked feature of C# (and OO programming in general) is the use of abstract classes to maximise code re-use. Put simply, if you have code in related classes that is the same but needs to call implementation specific methods, you can probably use an abstract class to consolidate this code. Lets dive right in with a simple example. </p>
<p>This is production code that has been stripped back to highlight the use of abstraction. In my scenario I had a number of user controls that had to implement the same functionality to each display three different views. The initialisation code ran through the same method calls in each control but the implementations of the called methods were different. Enter the abstract class &#8230;</p>

<div class="wp_codebox"><table><tr id="p2153"><td class="code" id="p215code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">class</span> ControlBase 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ViewMode <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetSaveButtonEnabled<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetCollapsedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetSummaryView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> SetExpandedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> HideAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> InitControl<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        SetViewMode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        SetSaveButtonEnabled<span style="color: #008000;">&#40;</span><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;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetViewMode<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        HideAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">switch</span> <span style="color: #008000;">&#40;</span>ViewMode<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">:</span>
                SetCollapsedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">:</span>
                SetSummaryView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">:</span>
            <span style="color: #0600FF; font-weight: bold;">case</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">:</span>
                SetExpandedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The InitView() and SetViewMode() methods are the same across all controls. Obviously the actual code to render the correct view is specific to the implementation of the control itself. So how does it work? Firstly, an abstract class can never be instantiated &#8230; it can only be inherited from. Any methods specific to the implementation are declared as abstract. In the abstract base class we know what the method signature should be but we can&#8217;t implement it at this level. Once the signature is provided and declared abstract we can call that method in the abstract class code as demonstrated in the SetViewMode() and InitControl() methods. At runtime this will result in the implementation of the method in the child class being called. </p>
<p>Our inherited class would look something like this &#8230;</p>

<div class="wp_codebox"><table><tr id="p2154"><td class="code" id="p215code4"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Control1 <span style="color: #008000;">:</span> ControlBase
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetSaveButtonEnabled<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetCollapsedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetSummaryView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetExpandedView<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> HideAll<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">//control specific code here </span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In the child class we override the abstract methods. Failing to override all the abstract methods on the base class will result in a compile time error unless your child class is also abstract. </p>
<p>That&#8217;s really all there is to it. By using this technique I saved having the InitControl() and SetViewMode() methods being implemented with the exact same code in all of my child controls. This would at best have been copy-paste coding which is horrible and to be avoided at all times. A bug in one of those methods would have resulted in needing to update all the implementations to fix it. Add the complexity of working in a team and another team member may not realise that the code is repeated in multiple controls. By using the abstract class there is only one implementation to maintain making maintenance much simpler while allowing a much more object oriented design. </p>
<p>More info on <a href="http://msdn.microsoft.com/en-us/library/sf985hc5.aspx" title="MSDN" target="_blank">MSDN</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2012/01/13/code-re-use-with-abstract-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Radio Buttons and Repeaters in Aspx</title>
		<link>http://www.idevforfun.com/index.php/2011/12/12/radio-buttons-and-repeaters/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=radio-buttons-and-repeaters</link>
		<comments>http://www.idevforfun.com/index.php/2011/12/12/radio-buttons-and-repeaters/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 16:04:44 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[radio button]]></category>
		<category><![CDATA[repeater]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=190</guid>
		<description><![CDATA[TweetIf you&#8217;re using a Repeater control and have a requirement to have radio buttons in the repeated rows you may find yourself in a head scratching mess. You may find that checking one radio button does not result in all other buttons being unchecked. The radio buttons will essentially start to behave like a bunch ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton190" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F12%2F12%2Fradio-buttons-and-repeaters%2F&amp;via=Lawnmower_Man&amp;text=Radio%20Buttons%20and%20Repeaters%20in%20Aspx&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F12%2F12%2Fradio-buttons-and-repeaters%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>If you&#8217;re using a Repeater control and have a requirement to have radio buttons in the repeated rows you may find yourself in a head scratching mess. You may find that checking one radio button does not result in all other buttons being unchecked. The radio buttons will essentially start to behave like a bunch of check boxes &#8230; not cool. </p>
<p>With normal html radio buttons we group them together using the &#8216;name&#8217; attribute. All radio buttons with the same value in the name attribute become mutually exclusive resulting in the familiar behaviour where only one radio button in that group can be checked at any one time. In an asp:RadioButton control the name attribute maps to the GroupName parameter which normally works fine. </p>
<p>Things start to go astray when the radio buttons are added to a repeater however. You may be aware that asp.net will insert its own unique id for each control which is not the same as the id you originally gave it in code or markup. It does this to ensure that any elements that have the same id end up with a unique id once rendered. In the case of the repeater this is very necessary as you are taking the same elements with the same id and emitting them multiple times. Unfortunately a repeater also mangles the name attribute of the controls. Normally this can be safely ignored but radio buttons rely on this attribute to work out which group they belong to. All of a sudden each radio button belongs to its own little group and so we end up with check box behaviour. </p>
<p>This is a known issue explained in detail <a href="http://support.microsoft.com/kb/316495" title="here" target="_blank">here</a>, however, Microsoft have not yet fixed this and give no indication of if or when it will be fixed.</p>
<p>So now what? Well &#8230; there are a couple of work-arounds, none of which are particularly elegant. </p>
<p>1) Avoid using the repeater or radio buttons and see if you can solve the problem with other controls. </p>
<p>2) If you&#8217;re stuck with repeaters and radio buttons the most elegant solution is to use javascript to de-select other radio buttons using the click event. </p>
<p>This goes in the &lt;head&gt; tag &#8230;</p>

<div class="wp_codebox"><table><tr id="p1907"><td class="code" id="p190code7"><pre class="javascript" style="font-family:monospace;">&nbsp;
 <span style="color: #006600; font-style: italic;">//I use jquery to make life easier but you don't have to </span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #003366; font-weight: bold;">function</span> MakeRbExclusive<span style="color: #009900;">&#40;</span>rb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> rbs <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#rbData :radio'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> rbs.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rbs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> rb<span style="color: #009900;">&#41;</span> rbs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>And our repeater &#8230;</p>

<div class="wp_codebox"><table><tr id="p1908"><td class="code" id="p190code8"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;rbData&quot;&gt;
    &lt;asp:Repeater ID=&quot;repeater&quot; runat=&quot;server&quot;&gt;
        &lt;ItemTemplate&gt;
            &lt;!-- I have bound to simple Dictionary&lt;string, string&gt; created in page load for example purposes --&gt;
            &lt;asp:Label ID=&quot;lbl&quot; runat=&quot;server&quot; AssociatedControlID=&quot;radio&quot;&gt;&lt;%#Eval(&quot;key&quot;)%&gt;&lt;/asp:Label&gt;
            &lt;asp:radioButton ID=&quot;radio&quot; runat=&quot;server&quot; value=&lt;%#Eval(&quot;value&quot;)%&gt; onclick=&quot;MakeRbExclusive(this);&quot;/&gt;
            &lt;br /&gt;
        &lt;/ItemTemplate&gt;
    &lt;/asp:Repeater&gt;
&lt;/div&gt;</pre></td></tr></table></div>

<p><br/><br />
3) Avoid the use of a repeater completely and add your controls dynamically from code behind in a loop to achieve the same result. I *really* don&#8217;t recommend this. It is finicky at best and takes very careful coding. Dynamically added controls are not automatically tracked in viewstate and can create big problems if not carefully coded. I have had to do this in the past but it is definitely an option I&#8217;d only consider once I have exhausted all others. </p>
<p><br/>Happy coding! <img src='http://www.idevforfun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/12/12/radio-buttons-and-repeaters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Barcellona to Morella, Spain</title>
		<link>http://www.idevforfun.com/index.php/2011/06/29/barcellona-to-morella-spain/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=barcellona-to-morella-spain</link>
		<comments>http://www.idevforfun.com/index.php/2011/06/29/barcellona-to-morella-spain/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 19:14:06 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Motorcycles]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=177</guid>
		<description><![CDATA[TweetWe&#8217;re touring through Spain for five weeks avoiding motorways like the black plague. This is the town of Morella in the background &#8230; as you can see the bike is fully laden &#8230; three box luggage kit, tank bag and we&#8217;re two up &#8230; you could say we&#8217;re making use of the &#8216;tourer&#8217; aspect of ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton177" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F29%2Fbarcellona-to-morella-spain%2F&amp;via=Lawnmower_Man&amp;text=Barcellona%20to%20Morella%2C%20Spain&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F29%2Fbarcellona-to-morella-spain%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>We&#8217;re touring through Spain for five weeks avoiding motorways like the black plague. This is the town of Morella in the background &#8230; as you can see the bike is fully laden &#8230; three box luggage kit, tank bag and we&#8217;re two up &#8230; you could say we&#8217;re making use of the &#8216;tourer&#8217; aspect of the bike&#8217;s design &#8230; </p>
<p><img src="http://img121.imageshack.us/img121/9093/dsc0488h.jpg" alt="Morella, Spain" style="width:480px; height:320px"/></p>
<p>This video is of us having a little fun on a nice little stretch of road between Barcelona and Morella &#8230; I have to give the bike credit here for still handling given the weight its got on it <img src='http://www.idevforfun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.youtube.com/watch?v=vhaDmqhEeTI">www.youtube.com/watch?v=vhaDmqhEeTI</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/06/29/barcellona-to-morella-spain/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Generic Singelton</title>
		<link>http://www.idevforfun.com/index.php/2011/06/09/generic-singelton/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=generic-singelton</link>
		<comments>http://www.idevforfun.com/index.php/2011/06/09/generic-singelton/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 13:55:21 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=126</guid>
		<description><![CDATA[TweetWe are often required to implement the singleton pattern in our code. While doing this yet again I thought there must be a generic way to do this and I came up with the following code &#8230; public static class Singleton&#60;T&#62; where T : class, new&#40;&#41; &#123; private static volatile T _instance; private readonly static ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton126" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F09%2Fgeneric-singelton%2F&amp;via=Lawnmower_Man&amp;text=Generic%20Singelton&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F09%2Fgeneric-singelton%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>We are often required to implement the singleton pattern in our code. While doing this yet again I thought there must be a generic way to do this and I came up with the following code &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12611"><td class="code" id="p126code11"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Singleton<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> volatile T _instance<span style="color: #008000;">;</span>
   <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> _lockObj <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> T Instance
   <span style="color: #008000;">&#123;</span>
       get
       <span style="color: #008000;">&#123;</span>
           <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
              <span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span>_lockObj<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_instance <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> _instance <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
           <span style="color: #0600FF; font-weight: bold;">return</span> _instance<span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
This allows you to use any class as a singleton &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12612"><td class="code" id="p126code12"><pre class="csharp" style="font-family:monospace;">var mySingleton <span style="color: #008000;">=</span> GenericSingleton<span style="color: #008000;">&lt;</span>MySingleton<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><br/><br />
It&#8217;s not perfect &#8230; there is nothing stopping the developer from creating a new instance of the class which breaks the singleton pattern. Doing it the normal way makes it impossible to create more than one instance of a class but this way allows the flexibility to use a class as a singleton even if it&#8217;s not implemented that way and also allows a mix of singleton and instances of the same class.</p>
<p>In any event it makes interesting use of generics and I just thought it was cool so I thought I&#8217;d share <img src='http://www.idevforfun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/06/09/generic-singelton/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Insert Multiple Rows With a Single Insert</title>
		<link>http://www.idevforfun.com/index.php/2011/06/08/insert-multiple-rows-with-a-single-insert/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=insert-multiple-rows-with-a-single-insert</link>
		<comments>http://www.idevforfun.com/index.php/2011/06/08/insert-multiple-rows-with-a-single-insert/#comments</comments>
		<pubDate>Wed, 08 Jun 2011 16:24:07 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Sql]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=166</guid>
		<description><![CDATA[TweetI often have to insert multiple rows of (usually) test data into tables. The simplest but most tedious way is to rewrite the INSERT statement over and over with different values &#8230; INSERT INTO User VALUES &#40;'Penny', 'Penny'&#41; INSERT INTO User VALUES &#40;'Sheldon', 'Cooper'&#41; INSERT INTO User VALUES &#40;'Leonard', 'Hofstadter'&#41; INSERT INTO User VALUES &#40;'Howard', ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton166" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F08%2Finsert-multiple-rows-with-a-single-insert%2F&amp;via=Lawnmower_Man&amp;text=Insert%20Multiple%20Rows%20With%20a%20Single%20Insert&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F06%2F08%2Finsert-multiple-rows-with-a-single-insert%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>I often have to insert multiple rows of (usually) test data into tables. The simplest but most tedious way is to rewrite the INSERT statement over and over with different values &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p16616"><td class="code" id="p166code16"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> User <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Penny'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Penny'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> User <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sheldon'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Cooper'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> User <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Leonard'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Hofstadter'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> User <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Howard'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Walowitz'</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> User <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Rajesh'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Koothrappali'</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><br/></p>
<p>Its not a big deal but as a developer I hate writing the same statement over and over &#8230; it just goes against my grain. Its also more of an overhead to repeatedly call insert for each row. Here&#8217;s a better way &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p16617"><td class="code" id="p166code17"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> User
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Penny'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Penny'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Sheldon'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Cooper'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Leonard'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Hofstadter'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Howard'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Walowitz'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> 
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Rajesh'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Koothrappali'</span></pre></td></tr></table></div>

<p><br/><br />
This inserts the same data as the first example but uses only one insert so has less overhead. If you happen to be running SQL Server 2008 there&#8217;s a new syntax to achieve the same although the above example will still work. In SQL Server 2008 you can use this syntax &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p16618"><td class="code" id="p166code18"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> User
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Penny'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Penny'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sheldon'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Cooper'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Leonard'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Hofstadter'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Howard'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Walowitz'</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Rajesh'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Koothrappali'</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/06/08/insert-multiple-rows-with-a-single-insert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Viewstate is not evil</title>
		<link>http://www.idevforfun.com/index.php/2011/05/15/viewstate-is-not-evil/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=viewstate-is-not-evil</link>
		<comments>http://www.idevforfun.com/index.php/2011/05/15/viewstate-is-not-evil/#comments</comments>
		<pubDate>Sun, 15 May 2011 16:46:41 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=129</guid>
		<description><![CDATA[TweetViewstate is often touted to be heavy and slow and better off disabled in most aspx applications. When used properly veiwstate can be quite a useful tool with not too much overhead. Use it improperly and all the horror stories you&#8217;ve heard start to come true &#8230; If you view page source in an aspx ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton129" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F15%2Fviewstate-is-not-evil%2F&amp;via=Lawnmower_Man&amp;text=Viewstate%20is%20not%20evil&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F15%2Fviewstate-is-not-evil%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Viewstate is often touted to be heavy and slow and better off disabled in most aspx applications. When used properly veiwstate can be quite a useful tool with not too much overhead. Use it improperly and all the horror stories you&#8217;ve heard start to come true &#8230;</p>
<p>If you view page source in an aspx application that has viewstate enabled site wide you will see the hidden __VIEWSTATE field will have a long encoded string of data. This is quite heavy and takes some time to trasmit betweeen the server and client. Our aim is to minimise the length of this data load to the point where overhead is no longer a concern. </p>
<p>First of all viewstate is enabled by default across the entire project so the first thing we need to do is turn it off. The idea is to opt in to viewstate rather than opt out. We only want to turn it on for the controls where we specifically want to maintain state. </p>
<p>In each masterpage or at the top of each page you can place this code in the page directive to disable the viewstate.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12922"><td class="code" id="p129code22"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> Page EnableViewState<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;False&quot;</span>  ViewStateMode<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Disabled&quot;</span><span style="color: #000000; font-weight: bold;">%&gt;</span></pre></td></tr></table></div>

<p><br/><br />
Or you can disable the viewstate across the whole application by putting this in the Pages tag in web config.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12923"><td class="code" id="p129code23"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>Pages EnableViewState<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;false&quot;</span>  <span style="color: #006600; font-weight: bold;">/&gt;</span></pre></td></tr></table></div>

<p><br/><br />
Now that Viewstate is off by default &#8230; we can choose which controls we want to maintain state for. Most text fields and simple controls will be fine without viewstate. As a general rule leave it off until you have a specific reason to turn it on. The give away will be when you try to access the value of a control in code behind and get the default value no matter what value was set. In this case its time to turn on viewstate for that control only. We do this by using the same code as we did to turn it off &#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p12924"><td class="code" id="p129code24"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>DropDownList runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;myDdl&quot;</span> EnableViewState<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;True&quot;</span>  ViewStateMode<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Enabled&quot;</span><span style="color: #006600; font-weight: bold;">/&gt;</span></pre></td></tr></table></div>

<p><br/><br />
You can add this to any control and access its value from a postback. If you view the page source again you will see the __VIEWSTATE field is much smaller &#8230; naturally the larger number of controls you enable viewstate for the larger the field will be.</p>
<p>Use it with caution but don&#8217;t write it off as bad practice.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/05/15/viewstate-is-not-evil/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bad Examples</title>
		<link>http://www.idevforfun.com/index.php/2011/05/07/bad-examples/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bad-examples</link>
		<comments>http://www.idevforfun.com/index.php/2011/05/07/bad-examples/#comments</comments>
		<pubDate>Sat, 07 May 2011 17:40:44 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=132</guid>
		<description><![CDATA[TweetAll over the web I see the code you see below. While it will work it is wrong &#8230; don&#8217;t do it! A web response stream is a stream like any other, you need to ensure that it is closed when you are finished with it or it will not release its resources. In the ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton132" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F07%2Fbad-examples%2F&amp;via=Lawnmower_Man&amp;text=Bad%20Examples&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F05%2F07%2Fbad-examples%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>All over the web I see the code you see below. While it will work it is wrong &#8230; don&#8217;t do it! A web response stream is a stream like any other, you need to ensure that it is closed when you are finished with it or it will not release its resources. In the case of a web response (or request for that matter), failing to close it could mean the server will run out of connections. In a high traffic web site this won&#8217;t take all that long. </p>
<p>Let&#8217;s take a look at the code &#8230; what would happen if the call to Write throws an exception? Execution would stop right there and the exception will get bubbled up the call stack. The call to Close will never happen and the response stream will remain open reducing the server&#8217;s connection capability by one.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p13228"><td class="code" id="p132code28"><pre class="csharp" style="font-family:monospace;"> var responseStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>byteData, <span style="color: #FF0000;">0</span>, byteData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
 responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><br/><br />
We can avoid this by putting the call to Close in a finally block as below. This would guarantee that the call to Close will be made even if an exception is thrown. This code is perfectly valid, but cumbersome to have to do every time you need to ensure a resource is released.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p13229"><td class="code" id="p132code29"><pre class="csharp" style="font-family:monospace;">var responseStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
    responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>byteData, <span style="color: #FF0000;">0</span>, byteData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">finally</span>
<span style="color: #008000;">&#123;</span>
    responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
The code can be rewritten as below to produce the same behaviour. The &#8216;using&#8217; keyword automatically calls Dispose (as good as calling Close) on the resource as soon as it falls out of scope resulting in the same behaviour as our finally block. Any time you use a stream, db connection or any resource that needs to be released, use a &#8216;using&#8217; statement and you can&#8217;t go wrong.<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p13230"><td class="code" id="p132code30"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var responseStream <span style="color: #008000;">=</span> response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    responseStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>byteData, <span style="color: #FF0000;">0</span>, byteData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/05/07/bad-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Releasing an app to the Windows Phone 7 marketplace</title>
		<link>http://www.idevforfun.com/index.php/2011/01/23/releasing-an-app-to-the-windows-phone-7-app-store/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=releasing-an-app-to-the-windows-phone-7-app-store</link>
		<comments>http://www.idevforfun.com/index.php/2011/01/23/releasing-an-app-to-the-windows-phone-7-app-store/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 16:53:25 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Musings]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=118</guid>
		<description><![CDATA[TweetWith the release of the new Windows Phone 7 I figured it might be time for me to get into the mobile market. I&#8217;ve been developing business systems for a long time now and the mobile platform offers me the ability to release some funky consumer based apps that I can actually sell. So I ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton118" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F01%2F23%2Freleasing-an-app-to-the-windows-phone-7-app-store%2F&amp;via=Lawnmower_Man&amp;text=Releasing%20an%20app%20to%20the%20Windows%20Phone%207%20marketplace&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2011%2F01%2F23%2Freleasing-an-app-to-the-windows-phone-7-app-store%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>With the release of the new Windows Phone 7 I figured it might be time for me to get into the mobile market. I&#8217;ve been developing business systems for a long time now and the mobile platform offers me the ability to release some funky consumer based apps that I can actually sell. So I figured I&#8217;d have a go &#8230; I wrote a simple app that was more of an exercise for me to learn the platform rather than something which was going to change the mobile world forever. It didn&#8217;t take long and I was ready for release &#8230; and this is when the pain started. </p>
<p>I submitted my app and after waiting a few days was notified that it had failed testing because the icon for the tile was missing should the user pin the app to their home screen. My bad .. an error in the resources file was causing the image to not be found .. easy fix. At this point I figured that the rest of testing must have been fine because this was nothing that would stop the app from being tested. Not so. I resubmitted only to be failed again because of a bug &#8230; fixed it &#8230; resubmitted .. failed again for another unrelated bug &#8230; fixed it .. failed again because I had no easy way of a user finding the version number or support contact. Sent an email to support asking for clarification as to what they wanted me to do for a user to be able to find this information &#8230; I have never received a reply. I gave up waiting and resubmitted with my email address in the support email field this time despite this being optional &#8230; failed again for the same reason &#8230; added an about screen and finally passed. Talk about a mission!</p>
<p>Now I don&#8217;t argue that the bugs weren&#8217;t mine .. they were .. and its a credit to Microsoft&#8217;s test team that they test so thoroughly. I do think its a bit rough that the testers take a test and stop approach even if the bug they find does not prevent further testing. Not only does it waste my time but it is certainly not the most efficient way for the Microsoft test team to carry out their work either. They had to test my app six times when they could have just done this twice. A missing icon did not in any way prevent testing. So why was it failed just for that with no further testing done until I resubmitted with the icon bug fixed? </p>
<p>In case you didn&#8217;t know &#8230; Microsoft allows only five free app <strong>submissions</strong> .. that is &#8230; each time an app fails testing you use one of your free submissions up. I, like may others, thought this meant five free apps can be released not just submitted. Naturally enough I was trying to release mine for free .. it was just an exercise after all<br />
 &#8230; but due to a testing process which stops and repeats at each bug &#8230; by the time I came to release it I either had to pay a fee to release it for free, or put a price on it. This makes my suspicious side believe that A) the testers are lazy and move on to the next app a soon as they have an excuse to reject the one they are testing and B) they do this on purpose to reduce the number of free submissions you have so that you either pay to release you app for free or you put a price on it. I put a price on it and am now quite confident no one will download it &#8230; the functionality does not justify even a small price.  </p>
<p>Considering that the only complaint I have heard about WP7 is the lack of good apps &#8230; you would think that microsoft would be working hard to get developers on board rather than treating them like second rate citizens. Knocking back an app repeatedly for each bug one by one is ridiculous when that bug does not prevent further testing. It demoralizes a developer, wastes our time and is ridiculously inefficient. </p>
<p>I am now working on an iPhone app &#8230; I might go back to a WP7 app later &#8230; I really should buy a device to test on. And this is my final problem .. I already have everything that I need to develop iPhone apps. To develop for WP7 I need to buy a device. This represents a decent investment which I may not recoup given the tiny market share held by WP7 at the moment. Given my experiences with app submission I am less likely to actually take this risk now due to the downright idiocy of the submission and testing process. </p>
<p>You did well with the OS Microsoft &#8230; it only took you ten years and a hiding from your competitors &#8230; don&#8217;t let a bad submission process screw you and your hopeful developers over &#8230; pick up your game &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2011/01/23/releasing-an-app-to-the-windows-phone-7-app-store/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linq to Objects Performance</title>
		<link>http://www.idevforfun.com/index.php/2010/09/09/linq-to-objects-performance/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq-to-objects-performance</link>
		<comments>http://www.idevforfun.com/index.php/2010/09/09/linq-to-objects-performance/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 21:11:32 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=103</guid>
		<description><![CDATA[TweetI have often wondered about Linq to objects. When we use a linq Select or FirstOrDefault statement what is the complexity of the call? Does it iterate through the entire list until it finds the object it is looking for in which case we are dealing with O(n) complexity? The other day I had to ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton103" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F09%2F09%2Flinq-to-objects-performance%2F&amp;via=Lawnmower_Man&amp;text=Linq%20to%20Objects%20Performance&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F09%2F09%2Flinq-to-objects-performance%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>I have often wondered about Linq to objects. When we use a linq Select or FirstOrDefault statement what is the complexity of the call? Does it iterate through the entire list until it finds the object it is looking for in which case we are dealing with O(n) complexity? The other day I had to write a small migration tool that answered my question. Linq to objects does in fact iterate through every item in a list until it stumbles across the object it is looking for. Take the following case&#8230;<br />
<br/></p>

<div class="wp_codebox"><table><tr id="p10332"><td class="code" id="p103code32"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> 
<span style="color: #0600FF; font-weight: bold;">private</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> list2<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">Public</span> LinqTester<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Populating lists ...&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	list1<span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	list2 <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">100000</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		list1<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		list2<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	list2<span style="color: #008000;">.</span><span style="color: #0000FF;">Sort</span><span style="color: #008000;">&#40;</span><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;">void</span> LoopTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Starting loop test ... &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	var start <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var num <span style="color: #0600FF; font-weight: bold;">in</span> list1<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		list2<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstOrDefault</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">==</span> num<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	var timeTaken <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span> <span style="color: #008000;">-</span> start<span style="color: #008000;">;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Time taken = {0}&quot;</span>, timeTaken<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: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> BinaryTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Starting binary test ... &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        var start <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var num <span style="color: #0600FF; font-weight: bold;">in</span> list1<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		list2<span style="color: #008000;">.</span><span style="color: #0000FF;">BinarySearch</span><span style="color: #008000;">&#40;</span>num<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	var timeTaken <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span> <span style="color: #008000;">-</span> start<span style="color: #008000;">;</span>
	Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Time taken = {0}&quot;</span>, timeTaken<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: #008080; font-style: italic;">//output</span>
<span style="color: #008080; font-style: italic;">//Starting loop test ... Time taken = 00:01:16.5160000</span>
<span style="color: #008080; font-style: italic;">//Starting binary test ... Time taken = 00:00:00.0170000</span></pre></td></tr></table></div>

<p><br/><br />
From this simple test we can see that using a binary search is by far more efficient than using linq to objects. When you think about it the compiler has no way of knowing if the list is sorted or how to compare the objects contained in the list if they are objects rather than primitives. It is impossible for the compiler to optimise the search so it can only do a linear search through the collection until it finds the item it is looking for. If you&#8217;re dealing with small collections this isn&#8217;t an issue but when the collections get large or if you&#8217;re concerned about performance then it&#8217;s worth considering whether linq is the appropriate choice. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/09/09/linq-to-objects-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio Power Commands</title>
		<link>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=visual-studio-power-commands</link>
		<comments>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 20:53:42 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[DotNet]]></category>

		<guid isPermaLink="false">http://www.idevforfun.com/?p=98</guid>
		<description><![CDATA[TweetPower Commands is a free and extremely useful plugin for visual studio. Among its many features is &#8216;Collapse All&#8217; so that you can collapse the solution tree in the solution explorer and &#8216;Open Command Window&#8217; to open a command window in the directory of the selected project. These are the two features I use the ]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton98" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F06%2F10%2Fvisual-studio-power-commands%2F&amp;via=Lawnmower_Man&amp;text=Visual%20Studio%20Power%20Commands&amp;related=&amp;lang=en&amp;count=vertical&amp;counturl=http%3A%2F%2Fwww.idevforfun.com%2Findex.php%2F2010%2F06%2F10%2Fvisual-studio-power-commands%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.idevforfun.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><a href="http://code.msdn.microsoft.com/PowerCommands">Power Commands</a> is a free and extremely useful plugin for visual studio. Among its many features is &#8216;Collapse All&#8217; so that you can collapse the solution tree in the solution explorer and &#8216;Open Command Window&#8217; to open a command window in the directory of the selected project. These are the two features I use the most and prove invaluable as the solution I use at work has 91 projects attached to it &#8230; this can be really painful to navigate when a number of the projects are expanded. Well worth the download &#8230; </p>
<p><a href="http://code.msdn.microsoft.com/PowerCommands">Download Here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.idevforfun.com/index.php/2010/06/10/visual-studio-power-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

