<?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>.Net Smoothie &#187; Development</title>
	<atom:link href="http://www.richardbushnell.net/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardbushnell.net</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 11:42:52 +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>Connecting to Azure Table Storage</title>
		<link>http://www.richardbushnell.net/2009/03/12/connecting-to-azure-table-storage/</link>
		<comments>http://www.richardbushnell.net/2009/03/12/connecting-to-azure-table-storage/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 13:52:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Azure table-storage]]></category>

		<guid isPermaLink="false">http://www.richardbushnell.net/index.php/2009/03/12/connecting-to-azure-table-storage/</guid>
		<description><![CDATA[After you finally manage to get Table Storage working (see nice tutorial here) you&#8217;ll try to deploy it to your staging environment. That means you need to change the configuration. There are three settings to use: AccountName, AccountSharedKey and TableStorageEndpoint. Unfortunately, the values you need for them aren&#8217;t what you would naturally expect. AccountName: This [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2009%2F03%2F12%2Fconnecting-to-azure-table-storage%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>After you finally manage to get Table Storage working (see nice tutorial here) you&#8217;ll try to deploy it to your staging environment. That means you need to change the configuration.</p>
<p>There are three settings to use: <strong>AccountName</strong>, <strong>AccountSharedKey</strong> and <strong>TableStorageEndpoint</strong>. </p>
<p>Unfortunately, the values you need for them aren&#8217;t what you would naturally expect.</p>
<p><strong>AccountName</strong>: This isn&#8217;t <em>your</em> account name, but the name of your storage account. (I guess that&#8217;s why it has to be a unique name at the time you create it.) On the summary page of your storage project you will only see the value you need in the list of endpoints. It&#8217;s the first part of each of the three domain names.</p>
<p><strong>AccountSharedKey</strong>: This is the Primary Access Key as found in the summary page of your storage project in the developer portal.</p>
<p><strong>TableStorageEndpoint</strong>: This isn&#8217;t the endpoint as described in the summary page, but rather a shortened version. Just lop off the AccountName part of the endpoint you use as described in the summary page. That should give you something like <strong>http://table.core.windows.net</strong>.</p>
<p>For more information see <a href="http://msdn.microsoft.com/en-us/library/dd203057.aspx">this article</a> on MSDN.</p>
<p>It is so time-consuming to get right, that it really makes sense to follow the advice in the documentation. First, test locally. Then test your local hosted app using the live Storage account. Finally, load your app and test that in the staging environment.</p>
<p>Saying that, I haven&#8217;t yet managed to get the second scenario to work. It just crashes on start-up. (Help!)</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2009%2F03%2F12%2Fconnecting-to-azure-table-storage%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2009/03/12/connecting-to-azure-table-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Windows Azure</title>
		<link>http://www.richardbushnell.net/2009/03/12/learning-windows-azure/</link>
		<comments>http://www.richardbushnell.net/2009/03/12/learning-windows-azure/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 10:01:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Azure cloud getting-started]]></category>

		<guid isPermaLink="false">http://www.richardbushnell.net/index.php/2009/03/12/learning-windows-azure/</guid>
		<description><![CDATA[I started to learn how to use the Azure Cloud Service from Microsoft this week. Currently it&#8217;s still in Tech Preview stage. Unfortunately you can tell that from the SDK documentation. Here&#8217;s some useful links to get you going: Screencasts: http://msdn.microsoft.com/en-us/azure/dd439432.aspx These are quite basic, but trust me, you need them to be basic to [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2009%2F03%2F12%2Flearning-windows-azure%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I started to learn how to use the Azure Cloud Service from Microsoft this week. Currently it&#8217;s still in Tech Preview stage. Unfortunately you can tell that from the SDK documentation.</p>
<p>Here&#8217;s some useful links to get you going:</p>
<ol>
<li><strong>Screencasts</strong>: <a title="http://msdn.microsoft.com/en-us/azure/dd439432.aspx" href="http://msdn.microsoft.com/en-us/azure/dd439432.aspx">http://msdn.microsoft.com/en-us/azure/dd439432.aspx</a>       <br />These are quite basic, but trust me, you need them to be basic to get you started. </li>
<li><strong>SDK</strong>: <a title="http://www.microsoft.com/downloads/details.aspx?FamilyID=80e3eabf-0507-4560-aeb6-d31e9a70a0a6&amp;displaylang=en" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=80e3eabf-0507-4560-aeb6-d31e9a70a0a6&amp;displaylang=en">http://www.microsoft.com/downloads/details.aspx?FamilyID=80e3eabf-0507-4560-aeb6-d31e9a70a0a6&amp;displaylang=en</a>       <br />Contains CHTM-style documentation, tools and samples. Don&#8217;t expect too much from the docs; they explain enough to get you confused, and then have an API reference. You need to unzip the samples and get into them to start understanding how everything fits together. </li>
<li><strong>Visual Studio Templates</strong>: <a title="http://www.microsoft.com/downloads/details.aspx?FamilyID=8e90b639-1ef0-4e21-bb73-fc22662911bc&amp;displaylang=en" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=8e90b639-1ef0-4e21-bb73-fc22662911bc&amp;displaylang=en">http://www.microsoft.com/downloads/details.aspx?FamilyID=8e90b639-1ef0-4e21-bb73-fc22662911bc&amp;displaylang=en</a>       <br />This gives you a set of project and item templates which you can use to create and publish Azure applications. Don&#8217;t worry about the extra projects it adds to a solution, or the config files. You will learn more about them later. </li>
<li>The <strong>Azure developer center</strong> on MSDN: <a title="http://msdn.microsoft.com/en-us/azure/default.aspx" href="http://msdn.microsoft.com/en-us/azure/default.aspx">http://msdn.microsoft.com/en-us/azure/default.aspx</a> </li>
</ol>
<p>Assuming you already <a href="http://go.microsoft.com/fwlink/?LinkID=129453" target="_blank">registered for Azure</a>, that&#8217;s all you really need to get started.</p>
<p>The biggest problem I found at first was deploying an app. Once you have generated an Azure project in Visual Studio, you expect to be able to publish it from Visual Studio too. Unfortunately you can&#8217;t, and it takes a little more effort. I&#8217;ll write more about that in another post.</p>
<p>I also needed help trying to understand what to focus on to get started. So here&#8217;s a big tip: Ignore .Net Services, Live Services, and SQL Data Services. They aren&#8217;t part of Azure per se. You can come back to them later. First you just need a hosted project and some storage &#8211; either blob storage or table storage. (There&#8217;s also queue storage, but I bet no one will want to use that straight away &#8211; it&#8217;s for tying two apps together, which no one will want to do at first.)</p>
<p>I recommend you <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=80e3eabf-0507-4560-aeb6-d31e9a70a0a6&amp;displaylang=en">download the SDK</a> and the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=8e90b639-1ef0-4e21-bb73-fc22662911bc&amp;displaylang=en">Visual Studio templates</a>, create yourself a &#8220;Web Role&#8221; project (which is really the equivalent of an ASP.Net project), and work on that. Then move onto table and blob storage. You can use the <a href="http://msdn.microsoft.com/en-us/azure/dd439432.aspx" target="_blank">screencasts</a> to help you.</p>
<p>Good luck getting started!</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2009%2F03%2F12%2Flearning-windows-azure%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2009/03/12/learning-windows-azure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Acrobatics : Lambdas and Expression Methods as a replacement for NVelocity</title>
		<link>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/</link>
		<comments>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 10:09:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ListView]]></category>
		<category><![CDATA[NVelocity]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/</guid>
		<description><![CDATA[I&#8217;ve been very quiet recently. (I&#8217;m trying to not be so loud, Scott. ) You see, I&#8217;ve been writing a lot of ASP.Net code for a site I&#8217;m working on. And, to be honest, I&#8217;ve been having a lot of trouble. The source code for .Net has been very helpful, and I&#8217;ve learnt a lot [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F06%2F02%2Fc-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I&#8217;ve been very quiet recently. (I&#8217;m trying to not be so <a href="http://www.hanselman.com/blog/ProfessionalismProgrammingAndPunditryAndSuccessAsAMetric.aspx" target="_blank">loud</a>, Scott. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) You see, I&#8217;ve been writing a lot of ASP.Net code for a site I&#8217;m working on. And, to be honest, I&#8217;ve been having a <em>lot</em> of trouble. The <a href="http://richardbushnell.net/index.php/2008/04/18/the-value-of-being-free-to-see-the-source/" target="_blank">source code for .Net</a> has been very helpful, and I&#8217;ve learnt a lot about what&#8217;s going on under the covers of ASP.Net because of it. </p>
<p>(Note: I won&#8217;t comment here on the quality of the code I&#8217;ve found &#8211; I&#8217;ll leave that up to you to judge. But in any case, I&#8217;ve been trying to build on top of it.)</p>
<p>One thing I&#8217;ve found to be important is the reliance on Web Controls. (It&#8217;s got something to do with javascript libraries, but that&#8217;s another story.) Getting away from the &quot;standard&quot; way to do ASP.Net isn&#8217;t easy though. Even the ninjas on the <a href="http://www.haacked.com/archive/2008/05/31/the-design-is-never-right-the-first-time.aspx" target="_blank">ASP.Net MVC</a> team <a href="http://www.haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx" target="_blank">seem to be having trouble</a>. However, with the magic of lambdas and extension methods in C#, I think I might have just about managed to get something usable. I thought I&#8217;d publish my work here, and see what comments I got.</p>
<p>I think it&#8217;s best to start with what my ASP.Net code looks like once I&#8217;ve got everything working. (Notice I still have some Web Controls in there, but that&#8217;s because I&#8217;ve not worked out how to do sorting of data without web controls yet.)</p>
<p>The inspiration for this was taken from the <a href="r" target="_blank">improvements made to NVelocity</a> by the gurus on the Castle Project. I thought it looked great, and I&#8217;d like something similar, but I didn&#8217;t really want to learn a whole new scripting language and integrate it into my working environment just for rendering a bit of HTML. So I built some C# classes to do a similar thing for me instead. It&#8217;s not as nice as NVelocity, but it&#8217;s okay for now.</p>
<p><strong>Warning</strong>: The following code may contain statements of a disturbing nature to more sensitive readers. We cannot be held responsible for any confusion, delusion or mental illness caused by this code.</p>
<p>It starts by taking a collection of Task objects, and calling the extension method &quot;ForEach&quot; on them:</p>
<pre class="code"><span style="background: #ffee62">&lt;%</span> Tasks.ForEach(sections =&gt; {
   sections.NoData = tasks =&gt; {
<span style="background: #ffee62">%&gt;
</span><span style="color: blue">   &lt;</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
</span>   Hey, you've got nothing to do.<span style="color: blue">&lt;/</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
   &lt;</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
</span><span style="background: #ffee62">&lt;%
</span>   };
   sections.BeforeAll = tasks =&gt; {
   <span style="background: #ffee62">%&gt;
</span>  <span style="color: blue">&lt;</span><span style="color: #a31515">table </span><span style="color: red">class</span><span style="color: blue">=&quot;task-list&quot;&gt;
    &lt;</span><span style="color: #a31515">tr </span><span style="color: red">class</span><span style="color: blue">=&quot;task-list-header&quot;&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;StartDate&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Started&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;DueOn&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Due&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;Priority&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Priority&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">LinkButton </span><span style="color: red">CssClass</span><span style="color: blue">=&quot;task-description&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Sort&quot; </span><span style="color: red">CommandArgument</span><span style="color: blue">=&quot;Title&quot;
          </span><span style="color: red">Text</span><span style="color: blue">=&quot;Description&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; /&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;</span><span style="color: blue">      &lt;</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">th</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">tr</span><span style="color: blue">&gt;
   </span><span style="background: #ffee62">&lt;%
</span>   };
     sections.Before = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;</span><span style="color: #a31515">tr </span><span style="color: red">class</span><span style="color: blue">=&quot;</span><span style="background: #ffee62">&lt;%</span>= this.tableCssClasses.Next() <span style="background: #ffee62">%&gt;</span><span style="color: blue">&quot;&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };
   sections.Each = task =&gt; {
    <span style="background: #ffee62">%&gt;</span>
    <span style="color: blue">&lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">='calendar calendar-icon-</span><span style="background: #ffee62">&lt;%</span>= task.StartMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">=&quot;calendar-day&quot;&gt;
          </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.StartDayOfMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">='calendar calendar-icon-</span><span style="background: #ffee62">&lt;%</span>= task.DueMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;
        &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">=&quot;calendar-day&quot;&gt;
          </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.DueDayOfMonth <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      </span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.Priority <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">td </span><span style="color: red">class</span><span style="color: blue">=&quot;task-title&quot;&gt;
      &lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">='</span><span style="background: #ffee62">&lt;%</span>= Href.For(&quot;~/Tasks/{0}/Show.aspx&quot;, task.ID) <span style="background: #ffee62">%&gt;</span><span style="color: blue">'&gt;</span><span style="background: #ffee62">&lt;%</span><span style="color: blue">= </span>task.Title <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
</span><span style="color: blue">    &lt;</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">asp</span><span style="color: blue">:</span><span style="color: #a31515">Button </span><span style="color: red">ID</span><span style="color: blue">=&quot;Button1&quot; </span><span style="color: red">runat</span><span style="color: blue">=&quot;server&quot; </span><span style="color: red">CssClass</span><span style="color: blue">=&quot;button&quot; </span><span style="color: red">CommandName</span><span style="color: blue">=&quot;Delete&quot; </span><span style="color: red">Text</span><span style="color: blue">=&quot;Mark Done&quot; /&gt;
    &lt;/</span><span style="color: #a31515">td</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };

   sections.After = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">tr</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };

   sections.AfterAll = task =&gt; {
    <span style="background: #ffee62">%&gt;
</span>    <span style="color: blue">&lt;/</span><span style="color: #a31515">table</span><span style="color: blue">&gt;
    </span><span style="background: #ffee62">&lt;%
</span>   };
 });
<span style="background: #ffee62">%&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>It might take a while to grasp what&#8217;s going on here. The code actually starts using an Extension method to IEnumerable that looks like this:</p>
<pre class="code"><span style="color: gray">public static void </span>ForEach&lt;T&gt;(<span style="color: gray">this </span><span style="color: #2b91af">IEnumerable</span>&lt;T&gt; enumerable, <span style="color: #2b91af">ForeachSectionSetter</span>&lt;T&gt; sectionSetter) {
  <span style="color: gray">if </span>(enumerable != <span style="color: gray">null</span>) {

    <span style="color: gray">if </span>(sectionSetter != <span style="color: gray">null</span>) {
      <span style="color: #2b91af">ForeachSections</span>&lt;T&gt; sections = <span style="color: gray">new </span><span style="color: #2b91af">ForeachSections</span>&lt;T&gt;();
      sectionSetter(sections);

      <span style="color: gray">if </span>(enumerable.Count() == 0) {
        <span style="color: gray">if </span>(sections.NoData != <span style="color: gray">null</span>)
          sections.NoData(enumerable);
        <span style="color: gray">return</span>;
      }

      <span style="color: gray">if </span>(sections.BeforeAll != <span style="color: gray">null</span>)
        sections.BeforeAll(enumerable);

      <span style="color: gray">int </span>itemIndex = 0;
      T previousItem = <span style="color: gray">default</span>(T);

      <span style="color: gray">foreach </span>(T item <span style="color: gray">in </span>enumerable) {
        <span style="color: gray">if </span>(sections.Before != <span style="color: gray">null</span>)
          sections.Before(item);
        <span style="color: gray">if </span>(itemIndex % 2 == 1 &amp;&amp; sections.Odd != <span style="color: gray">null</span>)
          sections.Odd(item);
        <span style="color: gray">if </span>(itemIndex % 2 == 0 &amp;&amp; sections.Even != <span style="color: gray">null</span>)
          sections.Even(item);
        <span style="color: gray">if </span>(itemIndex &gt; 0 &amp;&amp; sections.Between != <span style="color: gray">null</span>)
          sections.Between(previousItem, item);
        <span style="color: gray">if </span>(sections.Each != <span style="color: gray">null</span>)
          sections.Each(item);
        <span style="color: gray">if </span>(sections.After != <span style="color: gray">null</span>)
          sections.After(item);
        itemIndex++;
        previousItem = item;
      }
      <span style="color: gray">if </span>(sections.AfterAll != <span style="color: gray">null</span>)
        sections.AfterAll(enumerable);
    }
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The delegate ForEachSectionSetter is used by the calling method with a lambda expression. As a parameter it receives an ForeachSections object, which looks like this:</p>
<pre class="code"><span style="color: gray">public class </span><span style="color: #2b91af">ForeachSections</span>&lt;T&gt; {
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Each { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; BeforeAll { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Before { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T,T&gt; Between { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Odd { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; Even { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;T&gt; After { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; AfterAll { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
  <span style="color: gray">public </span><span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">IEnumerable</span>&lt;T&gt;&gt; NoData { <span style="color: gray">get</span>; <span style="color: gray">set</span>; }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The calling method gets the chance to set the properties of this class before it is returned to the constructor of the ForEach method for processing. And because each property is already preset to a default value (Null in this case), the constructor can use the ForeachSections object just like a set of default or optional parameters. The caller can simply set values to the properties it needs, and ignore the rest.</p>
<p>If I had tried this another way, using overloadable constructors, it would have led to multiple constructors with indistinguishable signatures. If I&#8217;d have used property initializers, I wouldn&#8217;t have been able to run the whole routine without requiring a second call to the object, which actually wasn&#8217;t possible. </p>
<p>Basically, I couldn&#8217;t think of another way to do it. </p>
<p>The properties of the ForeachSections object are all delegates too. That means that we can use them with lambdas, which gives us lambdas inside of a lambda. (Hmm, very confusing!)</p>
<p>So what do you think? Could <em>you</em> use something like this? Can you make it simpler? Leave me a comment if you can.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F06%2F02%2Fc-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/06/02/c-acrobatics-lambdas-and-expression-methods-as-a-replacement-for-nvelocity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy Data-loading with LINQ-to-SQL and LINQ-to-XML</title>
		<link>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</link>
		<comments>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 07:53:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LINQ-to-XML]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[VB.Net]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/</guid>
		<description><![CDATA[.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &#34;X&#34;-types, you can make working with XML really easy. VB.Net 9 takes it one step further, and lets you write XML in your code without strings. &#34;Hey Rich, that&#8217;s old news,&#34; I hear you say. &#34;And who&#8217;s interested in [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>.Net 3.5 had some nice tricks in it. LINQ-to-XML was one of them. With the new &quot;X&quot;-types, you can make working with XML really easy. </p>
<p>VB.Net 9 takes it one step further, and lets you write XML in your code without strings.</p>
<p>&quot;Hey Rich, that&#8217;s old news,&quot; I hear you say. &quot;And who&#8217;s interested in VB today anyway?&quot;</p>
<p>Well, apparently there are a lot of VB-er&#8217;s still out there. I am mainly a C# developer myself, but I found that VB was perfect for a problem I had recently -&#160; loading of XML data into a SQL Server table.</p>
<p><span id="more-98"></span></p>
<h2>SQL Server 2005 and XML Fields</h2>
<p>I recently came across the need to load a database with XML data fields in SQL Server 2005. I had a series of XML documents which all needed to be pumped into a table containing a field of type XML. I could have opened up SQL Server Management Studio and fired in a load of strings, but I was a bit concerned that the XML might not be formatted quite correctly, which would have caused problems in the loading process.</p>
<p>Another problem was that I wanted to be able to add dynamic XML. I wanted to generate some XML data on the fly. That&#8217;s not so easy with insert statements in SQL.</p>
<h2>LINQ-to-SQL and XML Fields</h2>
<p>Using my recently-acquired LINQ-to-SQL knowledge, I knew that the Entities generated for an XML field are translated to <strong>XElements</strong> &#8211; the new XML type in .Net 3.5. I also knew that VB 9 lets you create an <strong>XElement</strong> by using XML in code itself: </p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image15.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="200" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb11.png" width="445" border="0" /></a>&#160;</p>
<p>So I came up with a plan&#8230;</p>
<h2>Inserts with LINQ-to-SQL</h2>
<p>For data-loading, LINQ-to-SQL is magic. A simple Insert is a piece of cake.</p>
<p>First, create your <strong>DataContext</strong> using the Visual Studio template, then drag the table you are inserting to onto the <strong>DataContext</strong> canvas. That gives you an Entity class. You then can create an Entity in your code, call <strong>InsertOnSubmit</strong> on the <strong>DataContext</strong> table, then call <strong>SubmitChanges</strong> on the <strong>DataContext</strong> itself.</p>
<p>Using Object Property Initializers, you can make this really easy.</p>
<p>Here&#8217;s what it looks like with VB.Net using an XML datatype:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image13.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="631" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb9.png" width="475" border="0" /></a> </p>
<p>(Sorry to have torn the edge off the code there, but it wouldn&#8217;t fit nicely in my post at full width. XML can be a <em>little </em>long sometimes.)</p>
<p>The great thing here is that any malformed XML is highlighted (or even automatically corrected) for me by the VB compiler. I can also add parameters and generate lots of different XML using the familiar ASP syntax:</p>
<div style="width:480px;margin-right:100px">
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image14.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="112" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb10.png" width="374" border="0" /></a>&#160;</p>
</div>
<div style="clear:left">
<p>And as <strong>SubmitChanges</strong> uses a single transaction, the data-loading can easily be an all-or-nothing affair. In addition, any foreign key relationships can easily be taken care of by assigning objects to each other using standard object notation. The <strong>DataContext</strong> takes care of doing things in the correct order for you.</p>
</div>
<p>It&#8217;s all super-easy, and takes about 1 minute to get started.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F20%2Feasy-data-loading-with-linq-to-sql-and-linq-to-xml-4%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/20/easy-data-loading-with-linq-to-sql-and-linq-to-xml-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Design Guidelines for LINQ</title>
		<link>http://www.richardbushnell.net/2008/03/13/design-guidelines-for-linq/</link>
		<comments>http://www.richardbushnell.net/2008/03/13/design-guidelines-for-linq/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 18:40:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Guidelines]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/13/design-guidelines-for-linq/</guid>
		<description><![CDATA[Have you wondered if and when you should use the new LINQ features in .Net 3.5? Like, where should I put a new extension method? Should I use Func&#60;T&#62; or a custom delegate? How do I best implement a mix-in (extension methods on an interface)? Well, Mircea Trofin has just published a new draft of [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F13%2Fdesign-guidelines-for-linq%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Have you wondered if and when you should use the new LINQ features in .Net 3.5?</p>
<p>Like, where should I put a new extension method? Should I use Func&lt;T&gt; or a custom delegate? How do I best implement a mix-in (extension methods on an interface)?</p>
<p>Well, Mircea Trofin has just published a new draft of some <a href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx" target="_blank">LINQ design guidelines</a>. You might just find your answers there.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F13%2Fdesign-guidelines-for-linq%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/13/design-guidelines-for-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ExtensionMethod.net &#8211; An Extension Methods Database</title>
		<link>http://www.richardbushnell.net/2008/03/03/extensionmethodnet-an-extension-methods-database/</link>
		<comments>http://www.richardbushnell.net/2008/03/03/extensionmethodnet-an-extension-methods-database/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 20:32:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/03/extensionmethodnet-an-extension-methods-database/</guid>
		<description><![CDATA[While surfing around tonight, I came across ExtensionMethod.net, a database of useful Extension Methods for C# 3.0 and VB 9. I thought it might be useful, so I added a few of my own extension methods. IComparable&#60;T&#62;.LessThan int.Times int.To There aren&#8217;t many there yet, but there are one or two on there from Scott Guthrie. [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fextensionmethodnet-an-extension-methods-database%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>While surfing around tonight, I came across <a href="http://www.extensionmethod.net/" target="_blank">ExtensionMethod.net</a>, a database of useful Extension Methods for C# 3.0 and VB 9. I thought it might be useful, so I added a few of my own extension methods.</p>
<ul>
<li><a href="http://www.extensionmethod.net/" target="_blank">IComparable&lt;T&gt;.LessThan</a></li>
<li><a href="http://www.extensionmethod.net/Details.aspx?ID=85" target="_blank">int.Times</a></li>
<li><a href="http://www.extensionmethod.net/Details.aspx?ID=84" target="_blank">int.To</a></li>
</ul>
<p>There aren&#8217;t many there yet, but there are one or two on there from Scott Guthrie. </p>
<p>Have you got any code you could put up there? You could be one of the first if you go now.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fextensionmethodnet-an-extension-methods-database%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/03/extensionmethodnet-an-extension-methods-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Remove and Sort Those Ugly &#8220;using-Statements&#8221;</title>
		<link>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/</link>
		<comments>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 08:10:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Commands]]></category>
		<category><![CDATA[PowerCommands]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/03/remove-and-sort-those-ugly-using-statements/</guid>
		<description><![CDATA[Visual Studio 2008 has lots of goodies in it, like LINQ syntax, CSS editing, and testing tools. There&#8217;s a lesser-known feature which I really appreciate though &#8211; the &#8220;Remove and Sort Usings&#8221; command in the C# editor. You activate the command by placing your cursor over the using statements and clicking on the right mouse-button. [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fremove-and-sort-those-ugly-using-statements%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Visual Studio 2008 has lots of goodies in it, like LINQ syntax, CSS editing, and testing tools. There&#8217;s a lesser-known feature which I really appreciate though &#8211; the &#8220;Remove and Sort Usings&#8221; command in the C# editor.</p>
<p>You activate the command by placing your cursor over the using statements and clicking on the right mouse-button.</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image2.png"><img src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb.png" style="border: 0px none " alt="image" border="0" height="411" width="488" /></a></p>
<p><span id="more-65"></span></p>
<p>Using this groovy tool, I&#8217;ve been able to go through my old ugly C# files and get rid of all the superfluous using statements, then tidy them up, all at the click of a button. It&#8217;s something I always wanted to do, but had no time for, and didn&#8217;t want to pay for an extra tool to do it for me.</p>
<p>The <em>only </em>problem with the feature is that you have to go through every file and right-click on the &#8220;using&#8221; statements to tidy them up. That can get a bit tedious, but it&#8217;s worth it.</p>
<p>But now it&#8217;s the Visual Studio Extensibility Team to the rescue!</p>
<p>The VSX team at Microsoft have just released <a href="http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=df3f0c30-3d37-4e06-9ef8-3bff3508be31">PowerCommands for Visual Studio 2008</a>. It&#8217;s a free tool for Visual Studio with loads of new tools for making a VS-developer&#8217;s life easier. And one of those new tools is a Remove and Sort Usings command at the project level.</p>
<p>You activate it with a right-click on the project itself.</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/03/image3.png"><img src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image-thumb1.png" style="border: 0px none ; margin: 0pt 140px 0pt 0pt" alt="image" border="0" height="288" width="357" /></a></p>
<p>The tool then does the same Remove and Sort command for every file in the project.</p>
<p><strong>Tip: </strong>Make sure your project is buildable before using the tool. If there is an error stopping the build from running, you will get dialogs asking you to confirm every change in every file.</p>
<p>PowerCommands for Visual Studio 2008 also contains commands for:</p>
<ul>
<li>Collapse Projects</li>
<li>Copy Class</li>
<li>Paste Class</li>
<li>Copy References</li>
<li>Paste References</li>
<li>Copy As Project Reference</li>
<li>Edit Project File</li>
<li>Open Containing Folder</li>
<li>Open Command Prompt</li>
<li>Unload Projects</li>
<li>Reload Projects</li>
<li>Remove and Sort Usings</li>
<li>Extract Constant</li>
<li>Clear Recent File List</li>
<li>Clear Recent Project List</li>
<li>Transform Templates</li>
<li>Close All</li>
</ul>
<p>So, where can you get the tool?</p>
<p>The official site for PowerCommands is at the <a href="http://www.visualstudiogallery.com/ExtensionDetails.aspx?ExtensionID=df3f0c30-3d37-4e06-9ef8-3bff3508be31">Visual Studio Gallery</a>, but when you get there, you won&#8217;t see a download button. You actually have to click the &#8220;Learn More&#8221; button to go to the <a href="http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PowerCommands&amp;ReleaseId=559.">download page at the MSDN code gallery</a>.</p>
<p>You can read more about PowerCommands on the <a href="http://blogs.msdn.com/vsxteam/archive/2008/02/29/PowerCommands-for-Visual-Studio-2008-released.aspx">Visual Studio Team Extensibility blog</a>.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F03%2Fremove-and-sort-those-ugly-using-statements%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/03/remove-and-sort-those-ugly-using-statements/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Refactoring C# Series: Aggregation of IEnumerable</title>
		<link>http://www.richardbushnell.net/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/</link>
		<comments>http://www.richardbushnell.net/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 20:54:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 2.0]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Accumulator]]></category>
		<category><![CDATA[Aggregate]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/</guid>
		<description><![CDATA[I was recently reading Programming Ruby: The Pragmatic Programmers&#8217; Guide, Second Edition, and came across this piece of example Ruby code: [1,3,5,7].inject(0) {&#124;sum, element&#124; sum+element} -&#62; 16 [1,3,5,7].inject(1) {&#124;product, element&#124; product*element} -&#62; 105 Inject is a method which acts on an array by aggregating or accumulating the values within that array. It loops through the [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Frefactoring-c-series-aggregation-of-ienumerable%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.amazon.com/gp/product/0974514055?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0974514055"><img style="margin: 0px 20px 20px 0px" src="/images/21VJS5BMKVL._AA_SL160_.jpg" align="left" border="0" /></a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=0974514055" width="1" border="0" /></p>
<p>I was recently reading <a href="http://www.amazon.com/gp/product/0974514055?ie=UTF8&amp;tag=netsmoo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0974514055">Programming Ruby: The Pragmatic Programmers&#8217; Guide, Second Edition</a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=netsmoo-20&amp;l=as2&amp;o=1&amp;a=0974514055" width="1" border="0" />, and came across this piece of example Ruby code:</p>
<blockquote><p>[1,3,5,7].inject(0) {|sum, element| sum+element} -&gt; 16      <br />[1,3,5,7].inject(1) {|product, element| product*element} -&gt; 105</p>
</blockquote>
<p><strong>Inject</strong> is a method which acts on an array by <em>aggregating</em> or <em>accumulating</em> the values within that array. It loops through the array, and for every item in the array, it performs a function. It then saves the result for the next iteration of the loop and eventually returns the aggregated value.</p>
<p>In <strong>C# 1.0</strong> you would probably write such a method like this:</p>
<pre class="code"><span style="color: blue">int </span>sum = 0;
<span style="color: blue">int</span>[] list = <span style="color: blue">new int</span>[] { 1, 3, 5, 7 };
<span style="color: blue">foreach </span>(<span style="color: blue">int </span>item <span style="color: blue">in </span>list)
{
  <span style="color: green">// Perform some function, then save the result
  </span>sum = sum + item;
}</pre>
<p>It&#8217;s a bit long-winded, and if you wanted to make it reusable, you&#8217;d have a hard time.</p>
<p>In C# 3.0, you can do it just like you can in Ruby.</p>
<p><span id="more-60"></span></p>
<h2>Defining the Aggregation</h2>
<p>First, let me explain how the Ruby <strong>inject </strong>method works.</p>
<p>First of all, it&#8217;s really misnamed in Ruby. The method is an aggregator, or even an &quot;accumulator&quot;. It aggregates or accumulates values. It takes two parameters &#8211; a starter value, and a block. In C# 3.0 we basically call a block a delegate, although in this case, it&#8217;s more like a Lambda Expression.</p>
<p>The <strong>inject</strong> method works like this:</p>
<ol>
<li>it takes the starter parameter and sets the aggregated value (the <strong>sum</strong> in the above example) to its value. </li>
<li>For the first item in the array, it passes the tarter value and the item itself to the block, which then performs the defined function on it. </li>
<li>The return value from the block is then assigned to the running aggregated value. </li>
<li>The running aggregated value is passed into the next iteration with the next item from the list. The function is called on those items again. </li>
<li>When the method has iterated over all items in the list, it returns the aggregated value. </li>
</ol>
<h2>Moving to C# 2.0</h2>
<p>So, let&#8217;s imagine you were trying to write the <strong>Inject </strong>method in <strong>C# 2.0</strong>. If you were to refactor the above <strong>C# 1.0</strong> example as <strong>C# 2.0</strong>, you might use anonymous methods to write this:</p>
<pre class="code"><span style="color: green">// Define a delegate
</span><span style="color: blue">delegate int </span><span style="color: #2b91af">Aggregator</span>(<span style="color: blue">int </span>sum, <span style="color: blue">int </span>item);

<span style="color: blue">static void </span>Main(<span style="color: blue">string</span>[] args) {

  <span style="color: blue">int </span>sum = 0;
  <span style="color: blue">int</span>[] list = <span style="color: blue">new int</span>[] { 1, 3, 5, 7 };

  sum = Inject(sum, list,     <span style="color: blue">delegate</span>(<span style="color: blue">int </span>starterVal, <span style="color: blue">int </span>item) {
      <span style="color: blue">return </span>starterVal + item;
    });
}

<span style="color: blue">int </span>Inject(<span style="color: blue">int </span>starterVal, <span style="color: blue">int</span>[] list,
           <span style="color: #2b91af">Aggregator </span>aggregator) {
  <span style="color: blue">int </span>sum = starterVal;
  <span style="color: blue">foreach </span>(<span style="color: blue">int </span>item <span style="color: blue">in </span>list) {
    <span style="color: green">// Perform some function, then save the result
    </span>sum = aggregator(sum, item);
  }
  <span style="color: blue">return </span>sum;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>However, that would only work for <strong>int</strong> objects. You could really make it generic, and use the same method for other types:</p>
<pre class="code"><span style="color: green">// Define a delegate
</span><span style="color: blue">delegate </span>T <span style="color: #2b91af">Aggregator</span>&lt;T&gt;(T sum, T item);

<span style="color: blue">...</span></pre>
<p>
  </p>
<pre class="code"><span style="color: blue"></span>  sum = Inject&lt;<span style="color: blue">int</span>&gt;(sum, list,
     <span style="color: blue">delegate</span>(<span style="color: blue">int </span>starterVal, <span style="color: blue">int </span>item) {
      <span style="color: blue">return </span>starterVal + item;
    });</pre>
<p>
  </p>
<pre class="code">...

T Inject&lt;T&gt;(T starterVal, T[] list,
  <span style="color: #2b91af">Aggregator</span>&lt;T&gt; aggregator) {
  T sum = starterVal;
  <span style="color: blue">foreach </span>(T item <span style="color: blue">in </span>list) {
    <span style="color: green">// Perform some function, then save the result
    </span>sum = aggregator(sum, item);
  }
  <span style="color: blue">return </span>sum;
}</pre>
<p>It&#8217;s nicer, but the Ruby method still wins hands-down for neatness.</p>
<h2>Move Over Ruby, Here Comes C# 3.0</h2>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The good news is, the C# 3.0 compiler and the .Net 3.5 framework can now take care of most of this for you.</p>
<p>To start with, the Aggregator delegate is no longer necessary, because the new <strong>Func&lt;T,T,T&gt;</strong> delegate already defines a generic delegate which returns a value.</p>
<pre class="code">T Inject&lt;T&gt;(T starterVal, T[] list, <span style="color: #2b91af">Func</span>&lt;T,T,T&gt; aggregator)</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Secondly, the anonymous function using the delegate keyword can be replaced by a lambda expression like this:</p>
<pre class="code">(starterVal, item) =&gt; starterVal + item</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Thirdly, the Inject method can be defined as an extension method so that you can call it like this:</p>
<pre class="code">sum = list.Inject(sum, (starterVal, item) =&gt; starterVal + item );</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And fourthly, the compiler can infer the type for you, so you don&#8217;t need to specify it on calling the method.</p>
<h2>The Aggregate Method</h2>
<p>But the <em>even better news</em> is that this method is already defined in the .Net framework for you, so you don&#8217;t even have to write it. Instead of being called <strong>inject</strong>, it&#8217;s called <strong>Aggregate</strong>, and is defined on the <strong>System.Linq.Enumerable&lt;T&gt;</strong> class.</p>
<p>So basically, using C# 3.0, you could reduce the above code to this:</p>
<pre class="code"><span style="color: blue">var </span>sum = 0;
<span style="color: blue">var </span>list = <span style="color: blue">new</span>[] { 1, 3, 5, 7 };
sum = list.Aggregate(sum,
                     (starterVal, item) =&gt; starterVal + item );</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>You get an extra bonus point if you spotted the extra C# 3.0 features I sneaked in there &#8211; the var keyword, and array initializers.</p>
<p>C# 3.0 is pretty neat huh? Move over Ruby.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Frefactoring-c-series-aggregation-of-ienumerable%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/02/refactoring-c-series-aggregation-of-ienumerable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Pure ASP.Net Grid with Grouping</title>
		<link>http://www.richardbushnell.net/2008/03/02/a-pure-aspnet-grid-with-grouping/</link>
		<comments>http://www.richardbushnell.net/2008/03/02/a-pure-aspnet-grid-with-grouping/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 19:26:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Grouping]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LinqDataSource]]></category>
		<category><![CDATA[ListView]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/03/02/a-pure-aspnet-grid-with-grouping/</guid>
		<description><![CDATA[One of my favorite bloggers is Matt Berseth. Nearly once a week he comes up with a post where he does something amazing with the standard ASP.Net controls. I usually read his posts in awe. He&#8217;s really good. But he&#8217;s not only is a good developer, he&#8217;s a great writer. Even though his posts are [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Fa-pure-aspnet-grid-with-grouping%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>One of my favorite bloggers is <a href="http://mattberseth.com/blog">Matt Berseth</a>. Nearly once a week he comes up with a post where he does something amazing with the standard ASP.Net controls. I usually read his posts in awe. He&#8217;s really good.</p>
<p>But he&#8217;s not only is a good developer, he&#8217;s a great writer. Even though his posts are concise and straight to the point, most of them are pages long. You can really learn a lot from following his instructions.</p>
<p>He&#8217;s so good, in fact, that he regularly gets a mention on Scott Guthrie&#8217;s posts.</p>
<p>One of my favorite posts of his was where he used LINQ-to-SQL, a LinqDataSource control, and an ASP.Net ListView control &#8211; all new in .Net 3.5 &#8211; and made a grid with grouping functionality. He did it all in a standard way, and didn&#8217;t use any funny tricks.</p>
<p><a href="http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html"><img style="border-right: 0px; border-top: 0px; margin: 0px; border-left: 0px; border-bottom: 0px" height="350" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/03/image1.png" width="465" border="0" /></a> </p>
<p>Here&#8217;s a link to the article:</p>
<h5><a href="http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html">Building a Grouping Grid with the ASP.NET 3.5 LinqDataSource and ListView Controls</a></h5>
<p>Enjoy!</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F03%2F02%2Fa-pure-aspnet-grid-with-grouping%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/03/02/a-pure-aspnet-grid-with-grouping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The LinqDataSource and the Hidden Viewstate</title>
		<link>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</link>
		<comments>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 08:00:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[LinqDataSource]]></category>
		<category><![CDATA[ViewState]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/</guid>
		<description><![CDATA[Yesterday I thought I&#8217;d learn about the LinqDataSource in ASP.Net 3.5, and got an interesting surprise. The new LinqDataSource can also be used with a LINQ-to-SQL model to perform updates. You simply add the DataSource to your page, set the table name, and set EnableUpdate to true. Then, using a standard DataControl, you can make [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Yesterday I thought I&#8217;d learn about the <strong>LinqDataSource</strong> in ASP.Net 3.5, and got an interesting surprise.</p>
<p>The new <strong>LinqDataSource</strong> can also be used with a LINQ-to-SQL model to perform updates. You simply add the <strong>DataSource</strong> to your page, set the table name, and set <strong>EnableUpdate</strong> to true. Then, using a standard <strong>DataControl</strong>, you can make updates to your data entities.</p>
<p>The question is, how does this work? It appears to be a bit magical. <span id="more-55"></span></p>
<h2>The Magic of LINQ</h2>
<p>In a <a href="http://richardbushnell.net/index.php/2008/02/18/how-to-update-data-with-linq-to-sql/">previous post</a>, I mentioned that LINQ-to-SQL updates can be done in two ways: either you make a call to retrieve a row, then update it, or you provide known values for all fields and try to update using optimistic concurrency.</p>
<p>If the <strong>LinqDataSource</strong> used the first technique, the performance might be bad. Plus, it would have to only update those fields that had been updated by the <strong>DataControl</strong>, and ignore the rest. That wouldn&#8217;t make sense, so I correctly supposed that was not what was happening.</p>
<p>It makes more sense that the <strong>LinqDataSource</strong> would use the values input by the user to make the changes. It could use optimistic concurrency to compare all the entered values.</p>
<p>The problem is, I don&#8217;t always display all fields from a particular row on the form, so how would the <strong>LinqDataSource</strong> know what the missing fields were. Remember, all the fields involved in the update are required for the optimistic concurrency to work. That is usually all the fields in a row, and they aren&#8217;t usually all displayed.</p>
<p>So, have a guess. Where does the <strong>LinqDataSource</strong> store it&#8217;s values?</p>
<p>Let&#8217;s think now. Where would be the <em>easiest </em>place to keep it? To keep it really simple, I&#8217;ll let you ignore <a href="http://richardbushnell.net/index.php/2007/12/23/qualities-of-a-dot-net-application-design/">all other aspects of building an application</a>, like security or performance.</p>
<p>Yep, you guessed it &#8211; <strong>ViewState</strong>.</p>
<p>The ViewState actually contains all values for all the fields in a LINQ-to-SQL entity by default. Those values are sent to the client, even if the user isn&#8217;t supposed to be able to see them.</p>
<p>Aaaarrgghhhh!</p>
<p>Now, let&#8217;s not get overexcited. The ViewState is encoded, so it&#8217;s not easy to just go and change it. But it&#8217;s not impossible. In general, I don&#8217;t think it&#8217;s such a great idea to have all data sent to a client by default. Someone is going to overlook something one day, and there&#8217;ll be a costly mistake.</p>
<p>And what if you display a large grid using the LinqDataSource. Well, you aren&#8217;t just getting the ViewState from the grid sent back and forth with your page, but the LinqDataSource is going to store <em>all your data</em> retrieved in the ViewState too.</p>
<p>Bummer!</p>
<p>Great control. Large overhead.</p>
<h2>Kicking out the ViewState</h2>
<p>The good news is that the ASP.Net team realized that this was a problem and did something about it.</p>
<p>If you look carefully, you will notice that the <strong>LinqDataSource</strong> has a property <strong>StoreOriginalValuesInViewState</strong>. By default (sigh) that property is set to true. If you set it to false, the control won&#8217;t work any more. Simple as that.</p>
<p>Here&#8217;s what the official documentation has to say about that:</p>
<blockquote><p>If you set the <strong>StoreOriginalValuesInViewState</strong> property to false, the original values are not persisted in view state for the data-bound control. In that case, LINQ to SQL cannot verify the integrity of the data. LINQ to SQL will throw an exception that indicates a data conflict even if the data in the data source has not actually changed.</p></blockquote>
<p>That&#8217;s in the remarks section of the <strong>LinqDataSource</strong>.<strong>StoreOriginalValuesInViewState</strong> property documentation.</p>
<p>Funnily enough, the same section states the problem with stuffing data into the ViewState:</p>
<blockquote><p>Storing the original values in view state can cause the page size to become unnecessarily large and can expose sensitive data to a malicious user.</p></blockquote>
<p>Now, I don&#8217;t know about you, but that looks like a pretty serious warning to me. Why isn&#8217;t it in a more obvious place, like as a comment that pops in up Intellisense whenever you use a LinqDataSource?</p>
<h2>Check the UpdateCheck</h2>
<p>So how do you handle it? How can you use the <strong>LinqDataSource</strong>?</p>
<p>The answer lies in the generation of the LINQ-to-SQL model which you generate. Each entity field has a <strong>Column </strong>attribute which allows you to specify if it is used for checking during an optimistic concurrency field comparison.</p>
<p>For example, you could create a Model with the following Property:</p>
<pre class="code">[<span style="color: #2b91af">Column</span>(Storage=<span style="color: #a31515">"_AddressID"</span>, UpdateCheck=<span style="color: #2b91af">UpdateCheck</span>.Never, ...
<span style="color: blue">public int </span>AddressID
...</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Notice the <strong>UpdateCheck.Never </strong>value. That means that the field will not be used in the optimistic concurrency check.</p>
<p>The documentation helps a little again:</p>
<blockquote><p>By default, when update and delete operations have been enabled, the <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores the original values for all the records in view state. The <a href="ms-help://ms.vscc.v90/9b2f476d-7f28-2aa8-8143-3082edcf11d5.htm">LinqDataSource</a> control stores values for all primary keys and all properties not marked with UpdateCheck.Never in the Column attribute. You set the UpdateCheck property of the Column attribute in the O/R Designer.</p></blockquote>
<p>Admittedly, I haven&#8217;t yet tried that, so I don&#8217;t really know how if you can change that setting for a field without changing generated code, which would be a big no-no. I&#8217;ll just have to get back to you on that.</p>
<h2>Stamp In, Please</h2>
<p>However, there is a shortcut trick.</p>
<p>Basically, if you add a field of type <strong>timestamp</strong> to your record, only that field will be used for the concurrency check, and only that field&#8217;s data will be stored in the ViewState.</p>
<blockquote><p>If the underlying data source contains a timestamp field that is automatically updated during an update, you can store only that value in view state. In that case, the timestamp property in the entity class is set to IsVersion=true and all the properties are set to UpdateCheck.Never. Because a timestamp field is automatically updated by the database every time that data in that record changes, LINQ to SQL determines from that value if data has changed. This helps reduce the size of view state, and no sensitive data is exposed. LINQ to SQL will check for data consistency by comparing the timestamp value in view state with the timestamp value in the database.</p></blockquote>
<p>The documentation is great, if you know where to find it. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>So, the answer is, if you use the <strong>LinqDataSource</strong> to throw a quick application together, make sure you use a <strong>timestamp</strong> field in your table. Call it <strong>UpdatedOn</strong>, or something. It will make the use of the <strong>LinqDataSource</strong> a lot easier, if only because you&#8217;ll sleep better not worrying about it.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F02%2F21%2Fthe-linqdatasource-and-the-hidden-viewstate%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/02/21/the-linqdatasource-and-the-hidden-viewstate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Does the ASP.Net MVC Framework Frustrate You?</title>
		<link>http://www.richardbushnell.net/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/</link>
		<comments>http://www.richardbushnell.net/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 09:11:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[SubSonic]]></category>
		<category><![CDATA[trends]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/</guid>
		<description><![CDATA[Silverlight, ASP.Net Ajax, LINQ, Astoria, ASP.Net MVC &#8211; there&#8217;s just tons of stuff coming out of Microsoft. And that&#8217;s not to mention the stuff people have seemingly forgotten about &#8211; ASP.Net Futures with IronPython (AWOL), Patterns and Practices Web Client Software Factory, WPF, WF, WCF and CardSpaces. Now the trend seems to be functional programming [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F25%2Fdoes-the-aspnet-mvc-framework-frustrate-you%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>Silverlight, ASP.Net Ajax, LINQ, Astoria, ASP.Net MVC &#8211; there&#8217;s just tons of stuff coming out of Microsoft. And that&#8217;s not to mention the stuff people have seemingly forgotten about &#8211; ASP.Net Futures with IronPython (AWOL), Patterns and Practices Web Client Software Factory, WPF, WF, WCF and CardSpaces. Now the trend seems to be functional programming languages, especially with F#.</p>
<p>There&#8217;s just a bit <em>too</em> much for me.</p>
<p>What I really need is guidance. My customers don&#8217;t ask me what to they should use in 6 months; they ask me how to use what already exists. They don&#8217;t want &#8220;cool&#8221;; they want stability and consistency. And while the ball keeps moving, we can&#8217;t grab it.<br />
<span id="more-39"></span></p>
<p>Take for example, the ASP.Net Futures framework. I recently took over a project where it had been used. I checked to see what was available now &#8211; nothing! It seems to be dead. I&#8217;ll have to rip it out of the project, somehow. Major headache!</p>
<p>MSDN Patterns and Practices have produced some great stuff recently. Policy Injection, ObjectBuilder, PageFlows, Validators; but it&#8217;s completely ignored, as no one has a chance to look at it because of all the other stuff grabbing their attention. Well that, and the fact that it looks a bit overengineered.</p>
<p>While this is happening, and the cool guys are playing with all the new stuff, the guys on the ground are wondering what they should learn and what not. LINQ is hard enough to learn, and most developers haven&#8217;t even looked at the near 18-month-old WF suite yet. There just isn&#8217;t time.</p>
<p>I recently heard Dino Esposito say the same thing. I heard him on <a href="http://www.intellectualhedonism.com/2007/10/16/NETRocks281DinoEspositoOnAJAXArchitecture.aspx" target="_blank">DotNetRocks</a> bemoaning the fact that Microsoft seems to have abandoned ASP.Net Ajax to chase after Silverlight. They could have made ASP.Net Ajax absolutely fantastic, but aren&#8217;t doing anything about it any more. Seems like it&#8217;s finished.</p>
<p>But above it all, the MVC Framework just tips the scale for me.</p>
<p>Let me start with some history. About 18 months ago, I heard Scott Hanselman say in <a href="http://www.hanselminutes.com/default.aspx?showID=40" target="_blank">his podcast</a> that Ruby On Rails was <em>the framework</em> that Scott Guthrie and Microsoft should be worried about. I agreed. In fact, I went and learnt Rails because of that. I found it was really good, just like he said (apart from when you try to get a site deployed, at which point you need a degree in Rocket Science or a great hosting company. I had neither.) But the concepts I learnt from Rails were very interesting. I tried to apply them in my work.</p>
<p>Enter <a href="http://blog.wekeroad.com/2008/01/14/subsonic-and-mvc-introducing-makai/" target="_blank">SubSonic</a>. Rob Conery made a great implementation of the ActiveRecord pattern from Rails for .Net and called it SubSonic. He even included some of the syntactic sugar that makes Rails fun. It is a great framework, and I suggested to some customers that they use it. They tried. They failed. I asked them why, and they answered that it just didn&#8217;t go far enough. They went back to using DataAdapters.</p>
<p><a href="http://blog.wekeroad.com/2007/10/26/microsoft-subsonic-and-me/" target="_blank">Rob</a> and Scott both work for Scott Guthrie now. <a href="http://www.hanselman.com/blog/ASPNETMVCWebFormsUnplugged.aspx" target="_blank">They seemingly focus on the ASP.Net MVC Framework</a>, which, unsurprisingly, is a blatant attempt at a copy of Rails.</p>
<p>Whether or not it&#8217;s a good framework (and I think it is), the problem is, I need some features for the stuff I&#8217;ve already got. I might need Routes (ala Rails) for my ASP.Net apps; I don&#8217;t need a complete new system for building pages. It just took me 3 years to learn ASP.Net as it is now. I don&#8217;t want to throw it all away because it&#8217;s not cool any more. And I don&#8217;t think my customers want to do that neither.</p>
<p>Why not make ASP.Net better? Why not take the stuff in Patterns and Practices and make it simpler to understand. That&#8217;s what happened with C-Omega and C#3.0. The good stuff from a research project found its way into the next version of the more popular language. Thumbs up to Anders!</p>
<p>I think that Microsoft could actually do a much better job at Rails than DHH did. Sure, he gave them a kick in the butt, but that&#8217;s a good thing.</p>
<p>Rails has things in it which we don&#8217;t need in ASP.Net. Multiple views are often necessary for creating REST API&#8217;s &#8211; how often do ASP.Net developers do that? If we do, we have business objects and other ways to support an API. Routes using controllers is for nice-looking URLs, especially useful for search engines &#8211; how many pages of our sites do most ASP.Net developers want to post to search engines? MVC is to make it simpler to control what goes into markup, and what goes into a view &#8211; we already have MVP, which fits well in ASP.Net, and doesn&#8217;t lead to huge controller files. (What&#8217;s a controller anyhow?)</p>
<p>And don&#8217;t forget that Rails doesn&#8217;t have the concepts of security built in. It doesn&#8217;t have controls. Try integrating plugins &#8211; it&#8217;s not always easy. And despite the TextMate craze, the IDE is pitiful. You have to know Ruby well before you can do anything.</p>
<p>On the ASP.Net side, do we really want to kick out viewstate? It was put there for a purpose. Real-world website developers were having trouble managing the control of forms in ASP, and ViewState was a good answer. Rails doesn&#8217;t have that. It does everything with IDs in forms. Cool, but if it&#8217;s put into ASP.Net MVC, it means we&#8217;ll have to learn a completely new model to achieve the same purpose.</p>
<p>The real reason I&#8217;m peed off, is that I&#8217;m having to invent my own guidance, while MS seems to be messing about doing other things. That&#8217;s not good for the customers, as they get new ideas from everyone, and no idea what is right or wrong.</p>
<p>So guys, please. Why not stop what we&#8217;re doing, take a step back, consolidate, learn from each other, and build something better than ever.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F25%2Fdoes-the-aspnet-mvc-framework-frustrate-you%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/25/does-the-aspnet-mvc-framework-frustrate-you/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Calculating the Fibonacci Sequence with C# 3.0</title>
		<link>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/</link>
		<comments>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 09:36:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Fibonacci Sequence]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/</guid>
		<description><![CDATA[Scott Hanselman just posted his latest article in his weekly source code series. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons: I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F24%2Fcalculating-the-fibonacci-sequence-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.hanselman.com/blog" target="_blank">Scott Hanselman</a> just posted his <a href="http://www.hanselman.com/blog/TheWeeklySourceCode13FibonacciEdition.aspx" target="_blank">latest article in his weekly source code series</a>. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons:</p>
<ol>
<li>I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it out. (Doh!) </li>
<li>It makes me question what I thought about coding. </li>
</ol>
<p>You see, I spend a lot of time refactoring. Sometimes too much. I have to ask myself why. It&#8217;s usually so that I can come back to the code later, and still understand what I was trying to do. <em>Most</em> of the time, <em>conciser is better. </em>But not always!</p>
<p>And that&#8217;s what I see in this post.</p>
<p><span id="more-38"></span></p>
<p>Suppose I had written this C# function (shamelessly stolen from Scott&#8217;s post):</p>
<pre class="code"><span style="color: #2b91af">Func</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt; fib = <span style="color: blue">null</span>;
fib = n =&gt; n &gt; 1 ? fib(n - 1) + fib(n - 2) : n;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Compare this to the C# 2.0 version:</p>
<pre class="code"><span style="color: blue">static int </span>Fibonacci(<span style="color: blue">int </span>x) {
  <span style="color: blue">if </span>(x &lt;= 1)
    <span style="color: blue">return </span>1;
  <span style="color: blue">return </span>Fibonacci(x - 1) + Fibonacci(x - 2);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Apart from the fact that these methods actually give different answers (sorry, Scott), if I were to try to understand these methods 6 months after writing them, which one of the code samples would I be most glad of seeing again?</p>
<p>You see, most code only gets read again for two reasons:</p>
<ol>
<li>to debug it, or </li>
<li>to hand it over to someone else </li>
</ol>
<p>In both cases, clever though it may seem, the first conciser version would probably cause more trouble. Now, don&#8217;t get me wrong, I&#8217;m all for good concise code, but XSLT is concise, and I hate debugging that.</p>
<p>Now, considering that in order to reuse the <span style="color: #2b91af">Func</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt; function, I would have to pass the around, thus holding it in a static variable, or wrapping it in another class, it doesn&#8217;t actually turn out to be that practical after all. Concise, yes. Practical and readable, hmm.</p>
<p>So, while we have a very-much-by-Ruby-On-Rails-driven trend going to make code as beautiful as possible, don&#8217;t forget to not go too far. We have to be able to read code later on, remember.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F24%2Fcalculating-the-fibonacci-sequence-with-c-30%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/24/calculating-the-fibonacci-sequence-with-c-30/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using LinqPad to Create a Time-Selector Drop-Down List</title>
		<link>http://www.richardbushnell.net/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/</link>
		<comments>http://www.richardbushnell.net/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/#comments</comments>
		<pubDate>Fri, 18 Jan 2008 17:47:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[LinqPad]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/</guid>
		<description><![CDATA[I am really getting into LINQ now! I think it&#8217;s fantastic. I recently wanted to develop a quick drop-down list in ASP.Net which allows a user to select a time of day from a list. The times are 15 minutes apart, so the list would look like this: &#8230; 08:00 08:15 08:30 08:45 09:00 &#8230; [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F18%2Fusing-linqpad-to-create-a-time-selector-drop-down-list%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I am <em>really </em>getting into LINQ now! I think it&#8217;s fantastic. I recently wanted to develop a quick drop-down list in ASP.Net which allows a user to select a time of day from a list. The times are 15 minutes apart, so the list would look like this:</p>
<p>&#8230;<br /> 08:00<br /> 08:15<br /> 08:30<br /> 08:45<br /> 09:00<br /> &#8230;</p>
<p>&#8230; and so on.</p>
<p>Before LINQ, I would have done this with a for loop, like this:</p>
<pre class="code"><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt; times = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;();
<span style="color: blue">for </span>(<span style="color: blue">int </span>hour = 0; hour &lt; 24; hour++)
  <span style="color: blue">for </span>(<span style="color: blue">int </span>minute = 0; minute &lt; 60; minute++)
    <span style="color: blue">if </span>(minute % 15 == 0)
       times.Add(<span style="color: blue">string</span>.Format(
         <span style="color: #a31515">"{0:00}:{1:00}"</span>, hour, minute));</pre>
<p>That&#8217;s not difficult, although it&#8217;s not so easy to understand. I would have to write a small console app or test to make sure I had done it correct though.</p>
<p>I thought this might be a good opportunity to use <a href="http://www.linqpad.net" target="_blank">LinqPad</a>. It&#8217;s a great tool. You can use it to test a LINQ statement in a live window, so I thought I&#8217;d give it a go.</p>
<p>First I needed a LINQ statement to test.</p>
<p><span id="more-36"></span></p>
<h2>Designing the LINQ Statement</h2>
<p>The first thing I needed was an integer for the hour. That&#8217;s quite easy, especially if you use the Range Extension Method technique as I wrote about before. That method allows you to create an IEnumerable&lt;int&gt; from a simple statement, like 1.To(10). So to get the hours of the day, I can simply start with:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now I need to do a cross-join with the minutes. In C# comprehension syntax, you do that by simply adding another <font color="#0000ff">from</font> statement. At first, I thought I could do it by using an enumeration of minutes, specifying each value I wanted specifically. To do that I wrote this:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)
</span><span style="color: blue">from </span><span style="color: black">minute </span><span style="color: blue">in new</span><span style="color: black">[] {</span><span style="color: #c81efa">0</span><span style="color: black">, </span><span style="color: #c81efa">15</span><span style="color: black">, </span><span style="color: #c81efa">30</span><span style="color: black">, </span><span style="color: #c81efa">45</span><span style="color: black">}</span></pre>
<p><a href="http://11011.net/software/vspaste"></a>That works very nicely. I think it&#8217;s easy to understand too. Even if you were going to come back to it years from now, you could still easily see what is being done.</p>
<p>Another alternative is to do the same as the <font color="#0000ff">for</font> statement above, go through the numbers 0 to 59, selecting only those which are divisible by 15. You would do that using another range and a filter:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)
</span><span style="color: blue">from </span><span style="color: black">minute </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">59</span><span style="color: black">)
</span><span style="color: blue">where </span><span style="color: black">minute % </span><span style="color: #c81efa">15 </span><span style="color: black">== </span><span style="color: #c81efa">0</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Again, I like the way this looks. Another developer should have no difficulty reading that.</p>
<p>To select the string, we will also use the same format as before, this time using the <font color="#0000ff">select</font> keyword:</p>
<pre class="code"><span style="color: blue">select string</span><span style="color: black">.Format(</span><span style="color: #dc1414">"{0:00}:{1:00}"</span><span style="color: black">, hour, minute)</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>You then simply assign the value of this statement to a variable, like this:</p>
<pre class="code"><span style="color: blue">var </span>times = <span style="color: blue">from </span>hour <span style="color: blue">in </span>0.To(23)
            <span style="color: blue">from </span>minute <span style="color: blue">in </span>0.To(59)
            <span style="color: blue">where </span>minute % 15 == 0
            <span style="color: blue">select string</span>.Format(<span style="color: #a31515">"{0:00}:{1:00}"</span>, hour, minute);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h2>Using LinqPad to Test the Result</h2>
<p>Now, if you haven&#8217;t already, go <a href="http://www.linqpad.net/" target="_blank">download LinqPad from here</a>, and open it up. You will see a window to write a statement in the top right.</p>
<p>Before you continue, you need to add the extension methods to use my &#8220;To()&#8221; method. As a treat, just for you, <a href="http://www.richardbushnell.net/wp-content/uploads/2008/01/richardbushnellextensions.zip" target="_blank">here&#8217;s a simple assembly and a code file</a> you can use straight away. Download it, save the binary dll somewhere, then do the following in LinqPad:</p>
<ol>
<li>From the menu, click &#8220;Query&#8221;, then &#8220;Advanced Properties&#8221; (or press F4).
<li>On the Additional References tab, click the &#8220;Add&#8230;&#8221; button.
<li>Click the &#8220;Browse&#8230;&#8221; button on the window which pops up.
<li>Go to the location you saved RichardBushnell.Extensions.dll to, and select it. You should see it added to the list of additional references.
<li>Change to the Additional Namespace Imports tab.
<li>Type &#8220;RichardBushnell.Extensions&#8221;
<li>Click &#8220;OK&#8221;. </li>
</ol>
<p>Now, put your cursor in the Query1 tab, and paste in the following:</p>
<pre class="code"><span style="color: blue">from </span><span style="color: black">hour </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">23</span><span style="color: black">)
</span><span style="color: blue">from </span><span style="color: black">minute </span><span style="color: blue">in </span><span style="color: #c81efa">0</span><span style="color: black">.To(</span><span style="color: #c81efa">59</span><span style="color: black">)
</span><span style="color: blue">where </span><span style="color: black">minute % </span><span style="color: #c81efa">15 </span><span style="color: black">== </span><span style="color: #c81efa">0
</span><span style="color: blue">select string</span><span style="color: black">.Format(</span><span style="color: #dc1414">"{0:00}:{1:00}"</span><span style="color: black">, hour, minute)</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Press F5, and you will see a generated list like this:</p>
<p><a href="http://www.richardbushnell.net/wp-content/uploads/2008/01/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="176" alt="image" src="http://www.richardbushnell.net/wp-content/uploads/2008/01/image-thumb2.png" width="244" border="0"></a></p>
<p>And that&#8217;s it! You can now see <em>exactly</em> what you will get when you run this code.</p>
<p>What do you think? I&#8217;d love to hear your comments.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F18%2Fusing-linqpad-to-create-a-time-selector-drop-down-list%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/18/using-linqpad-to-create-a-time-selector-drop-down-list/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using Lambda Expressions with LINQ to SQL</title>
		<link>http://www.richardbushnell.net/2008/01/16/using-lambda-expressions-with-linq-to-sql/</link>
		<comments>http://www.richardbushnell.net/2008/01/16/using-lambda-expressions-with-linq-to-sql/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 13:43:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/16/using-lambda-expressions-with-linq-to-sql/</guid>
		<description><![CDATA[When using LINQ, you need to be careful to use the right kind of Lambda expression. &#8220;What, there is more than one kind?&#8221;, I hear you gasp. There sure is! And if you aren&#8217;t careful, you&#8217;ll get a nice little message at runtime to tell you: &#8220;System.Object DynamicInvoke(System.Object[])&#8217; has no supported translation to SQL.&#8221; What [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F16%2Fusing-lambda-expressions-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>When using LINQ, you need to be careful to use the right kind of Lambda expression. &#8220;What, there is more than one kind?&#8221;, I hear you gasp. There sure is! And if you aren&#8217;t careful, you&#8217;ll get a nice little message at runtime to tell you:</p>
<blockquote><p>&#8220;System.Object DynamicInvoke(System.Object[])&#8217; has no supported translation to SQL.&#8221;</p>
</blockquote>
<p>What that literally means is &#8220;You&#8217;re not using lambdas right, you dummy!&#8221;</p>
<p>Let me explain.</p>
<p><span id="more-31"></span></p>
<h2>A Refactoring Problem</h2>
<p>Let&#8217;s start with my problem. I created a Controller class which uses a DataContext to check for an item in a database, and then, if the item was not found, it inserts one. The class looks like this:</p>
<pre class="code"><span style="color: blue">  public class </span><span style="color: #2b91af">UserController </span>{
    <span style="color: blue">public void </span>CreateIDUser(<span style="color: blue">string </span>ID) {
      <span style="color: blue">var </span>DB = <span style="color: blue">new </span><span style="color: #2b91af">DBataContext</span>();
      <span style="color: blue">var </span>existingUsers = <span style="color: blue">from </span>user <span style="color: blue">in </span>DB.Users
                          <span style="color: blue">where </span>user.ID == ID
                          <span style="color: blue">select </span>user;

      <span style="color: blue">if </span>(existingUsers.Count() == 0) {
        <span style="color: #2b91af">User </span>newUser = <span style="color: blue">new </span><span style="color: #2b91af">User</span>();
        newUser.ID = ID;
        DB.Users.InsertOnSubmit(newUser);
        DB.SubmitChanges();
      }
    }
  }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The method uses LINQ C# Comprehension Syntax (&#8220;<span style="color: blue">from </span>user <span style="color: blue">in </span>DB.Users &#8230;&#8221;) to perform a query. It then uses the DataContext to do an Insert when the changes are submitted.</p>
<p>I created a unit test for this method, and it gave me a glowing green light.</p>
<p>Now, I want to add a similar method which uses a different property for comparison. The method would be identical to the first, except for the &#8220;where&#8221; query and the initialization of the new User object. I don&#8217;t want to repeat myself, as that might lead to bugs, so I want to refactor out the common code. I&#8217;m going to do it using lamda expressions.</p>
<h2>Refactoring using lambda expressions</h2>
<p>I could go into my design decisions here, but that&#8217;s not the point of this post. I&#8217;ll just give you the answer &#8211; what I want my class to look like.</p>
<p>Basically, I want to end up with 2 methods which only differentiate on the parts I need them too:</p>
<pre class="code"><span style="color: blue">public void </span>CreateIDUser(<span style="color: blue">string </span>ID) {
  CreateUser(
    user =&gt; user.ID == ID,
    user =&gt; user.ID = ID);
}

<span style="color: blue">public void </span>CreateNamedUser(<span style="color: blue">string </span>name) {
  CreateUser(
    user =&gt; user.Name == name,
    user =&gt; user.Name = name);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I create a private method called CreateUser which does the repetitive work:</p>
<pre class="code"><span style="color: blue">private void </span>CreateUser(<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">User</span>, <span style="color: blue">bool</span>&gt; compare, <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">User</span>&gt; initialize) {
  <span style="color: blue">var </span>DB = <span style="color: blue">new </span><span style="color: #2b91af">DBDataContext</span>();
  <span style="color: blue">var </span>existingUsers = <span style="color: blue">from </span>user <span style="color: blue">in </span>DB.Users
                      <span style="color: blue">where </span>compare(user)
                      <span style="color: blue">select </span>user;

  <span style="color: blue">if </span>(existingUsers.Count() == 0) {
    <span style="color: #2b91af">User </span>newUser = <span style="color: blue">new </span><span style="color: #2b91af">User</span>();
    initialize(newUser);
    DB.Users.InsertOnSubmit(newUser);
    DB.SubmitChanges();
  }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now, this compiles, but when I run my existing test, I get a nasty red light, and the exception message:</p>
<blockquote>
<p>&#8220;System.Object DynamicInvoke(System.Object[])&#8217; has no supported translation to SQL.&#8221;</p>
</blockquote>
<p>What&#8217;s wrong? The problem lies in the query itself, which is using expressions, not pure delegates. My Func&lt;User, bool&gt; cannot be used by LINQ to SQL to do its work. Instead, I have to use an Expression.</p>
<h2>Using an Expression&lt;Func&lt;T, bool&gt;&gt;</h2>
<p>Instead of a simple &#8220;Func&lt;User, bool&gt;&#8221;, I have to declare my parameter as &#8220;Expression&lt;Func&lt;User, bool&gt;&#8221;. That is what LINQ to SQL expects to use for deferred queries. The good news is that I can use an expression by defining it in the method accepting the lamda; the C# compiler can translate my existing lamda expressions from delegates to expressions without any extra work.</p>
<p>Once that&#8217;s done, there&#8217;s one more caveat. You cannot use C# comprehension syntax with an expression. You have to use the standard LINQ query syntax instead.</p>
<p>Here&#8217;s what the final class looks like after the refactoring.</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">UserController </span>{
  <span style="color: blue">public void </span>CreateIDUser(<span style="color: blue">string </span>ID) {
    CreateUser(
      user =&gt; user.ID == ID,
      user =&gt; user.ID = ID);
  }

  <span style="color: blue">public void </span>CreateNamedUser(<span style="color: blue">string </span>name) {
    CreateUser(
      user =&gt; user.Name == name,
      user =&gt; user.Name = name);
  }

  <span style="color: blue">private void </span>CreateUser(<span style="color: #2b91af">Expression</span>&lt;<span style="color: #2b91af">Func</span>&lt;<span style="color: #2b91af">User</span>, <span style="color: blue">bool</span>&gt;&gt; compare, <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">User</span>&gt; initialize) {
    <span style="color: blue">var </span>MeetUp = <span style="color: blue">new </span><span style="color: #2b91af">MeetUpDataContext</span>();
    <span style="color: #2b91af">IQueryable</span>&lt;<span style="color: #2b91af">User</span>&gt; existingUsers = MeetUp.Users
      .AsQueryable&lt;<span style="color: #2b91af">User</span>&gt;()
      .Where(compare);

    <span style="color: blue">if </span>(existingUsers.Count() == 0) {
      <span style="color: #2b91af">User </span>newUser = <span style="color: blue">new </span><span style="color: #2b91af">User</span>();
      initialize(newUser);
      MeetUp.Users.InsertOnSubmit(newUser);
      MeetUp.SubmitChanges();
    }
  }
}</pre>
<p>After that, I get my green light back, and we&#8217;re all happy.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F16%2Fusing-lambda-expressions-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/16/using-lambda-expressions-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Inserting into a Table with LINQ to SQL</title>
		<link>http://www.richardbushnell.net/2008/01/15/inserting-into-a-table-with-linq-to-sql/</link>
		<comments>http://www.richardbushnell.net/2008/01/15/inserting-into-a-table-with-linq-to-sql/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 13:40:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/15/inserting-into-a-table-with-linq-to-sql/</guid>
		<description><![CDATA[While creating a little project, I wanted to know how to insert an object into a table using LINQ to SQL. Using Intellisense it wasn&#8217;t obvious at all. Scott Guthrie wrote a post on how do it. Unfortunately, the method name has changed since he did it, so you need to call InsertOnSubmit on the [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F15%2Finserting-into-a-table-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>While creating a little project, I wanted to know how to insert an object into a table using LINQ to SQL. Using Intellisense it wasn&#8217;t obvious at all.</p>
<p><a href="http://weblogs.asp.net/scottgu" target="_blank">Scott Guthrie</a> wrote <a href="http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx" target="_blank">a post on how do it</a>. Unfortunately, the method name has changed since he did it, so you need to call InsertOnSubmit on the Table instead of Add, as he described it.</p>
<p>Phew! Getting that far was hard enough!</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F15%2Finserting-into-a-table-with-linq-to-sql%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/15/inserting-into-a-table-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Efficient Software Development with Visual Studio Team System 2008</title>
		<link>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/</link>
		<comments>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 10:57:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Team System]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[.Net Visual Studio Team System 2008 development]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/</guid>
		<description><![CDATA[I was recently asked to do some research and give a presentation about using Visual Studio Team System 2008. I thought I&#8217;d experiment with Slideshare.net and post it here. The presentation was for a large corporation, and will possibly not come across as being very useful for an online presentation. I have been working on [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F12%2Fefficient-software-development-with-visual-studio-team-system-2008%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>I was recently asked to do some research and give a presentation about using Visual Studio Team System 2008. I thought I&#8217;d experiment with Slideshare.net and post it here.</p>
<object width="400" height="328"><param name="movie" value="http://static.slideshare.net/swf/ssplayer2.swf?doc="/><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc="  type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="400" height="328"></embed></object>
<p>The presentation was for a large corporation, and will possibly not come across as being very useful for an online presentation. I have been working on it for a while though, and I think it&#8217;s worth publishing, if only as an introduction.</p>
<p>I&#8217;ve used competitive products to Team System for a long time, mainly open source:</p>
<ul>
<li>SubVersion for source version control
<li>CruiseControl.Net as a build server
<li>NAnt for build scripts
<li>MbUnit for testing
<li>NCover for seeing my testing code coverage
<li>VersionOne for project planning, task management, tracking and reporting
<li>BugTracker for bug tracking
<li>A Wiki for project documentation and guidelines </li>
</ul>
<p>I wasn&#8217;t all-too impressed with the standard of Visual Studio Team Suite 2005, but I am a little bit happier with the 2008 version. It took a while to get it installed so that I could test it, and there were lots of seemingly undocumented points which hindered my progress, but once I&#8217;d got it up-and-running, I didn&#8217;t find it that bad. (I did have to install it on&nbsp; virtual machine, and run it on my laptop.)</p>
<p>I think the best thing about Team System is that all the things I need to do are now integrated into one place. I didn&#8217;t have to go make changes to a dozen XML files in order to get a build server working. I didn&#8217;t have to install a Wiki for the project documentation. Using Team System would also avoid resistance to using the products, as I&#8217;ve faced before trying to get other team members to use such tools. Everything would just be there, out-of-the-box, and no one would argue about using them.</p>
<p>What I didn&#8217;t like about Team System are some of the subtle details which don&#8217;t work as they should do. I did a basic test of multiple check-outs, using the new features, but it didn&#8217;t work. The Unit Tests that I generated were confusing and were buggy.</p>
<p>The biggest problem though is the price. It&#8217;s darn expensive. I wouldn&#8217;t be able to afford Team System if I was just a small development shop. However, for a large corporation, it really could make sense.</p>
<p>I actually wonder if there&#8217;s room for a better competitor (Rational is possibly the only serious contender, and I find their software pretty naff). It actually wouldn&#8217;t to be too hard for some Python hackers somewhere. <img src='http://www.richardbushnell.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F12%2Fefficient-software-development-with-visual-studio-team-system-2008%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/12/efficient-software-development-with-visual-studio-team-system-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Can You Pass an Anonymous Type Across Functions?</title>
		<link>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/</link>
		<comments>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 09:22:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Anonymous Types]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[passing anonymous types functions]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/08/can-you-pass-an-anonymous-type-across-functions/</guid>
		<description><![CDATA[One of the biggest questions with Anonymous Types is &#8220;can I pass them around?&#8221; If not, why not? Can you do something like this, for example: var GetAnonymousValue() { return new { Name = "Richard Bushnell" }; } void Main() { var value = GetAnonymousValue(); var name = value.Name;} The answer is simple: no, you [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fcan-you-pass-an-anonymous-type-across-functions%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p>One of the biggest questions with Anonymous Types is &#8220;can I pass them around?&#8221; If not, why not? Can you do something like this, for example:</p>
<pre class="code"><font color="#0000ff">var</font> GetAnonymousValue() {
  <span style="color: blue">return new </span>{ Name = <span style="color: #a31515">"Richard Bushnell" </span>};
}

<span style="color: blue">void </span>Main() {
  <span style="color: blue">var </span>value = GetAnonymousValue();
  <span style="color: blue">var </span>name = value.Name;}</pre>
<p>The answer is simple: no, you can&#8217;t pass anonymous types across functions. var is not a dynamic variable, like in JavaScript. The CLR knows nothing about &#8220;var&#8221;, as the compiler just uses it to infer types when a variable is initialized.</p>
<p>At least, that was the answer until now. Using a simple extension method and generics, <a href="http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx" target="_blank">Alex James</a> just showed a nice way to pass them around on <a href="http://blogs.msdn.com/alexj/archive/2007/11/22/t-castbyexample-t-object-o-t-example.aspx" target="_blank">his blog</a>.</p>
<p>The trick is to use an example of the anonymous type you expect.</p>
<pre class="code"><span style="color: blue">object </span>GetAnonymousValue() {
  <span style="color: blue">return new </span>{ Name = <span style="color: #a31515">"Richard Bushnell" </span>};
}

<span style="color: blue">void </span>Main() {
  <span style="color: blue">var </span>value = GetAnonymousValue().CastByExample(<span style="color: blue">new </span>{ Name = <span style="color: #a31515">"" </span>});
  <span style="color: blue">var </span>name = value.Name;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>While this might be a little dangerous, especially if you&#8217;re not testing your code regularly, this could be the solution to a few problems I can already think of. </p>
<p>Watch out for casting exceptions at runtime though! If you make a mistake anywhere, you&#8217;ll won&#8217;t get a compile-time exception, but a nasty runtime exception instead.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fcan-you-pass-an-anonymous-type-across-functions%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/08/can-you-pass-an-anonymous-type-across-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Are you keeping up with software development?</title>
		<link>http://www.richardbushnell.net/2008/01/08/are-you-keeping-up-with-software-development/</link>
		<comments>http://www.richardbushnell.net/2008/01/08/are-you-keeping-up-with-software-development/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 06:25:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quaility]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[changes in software development]]></category>

		<guid isPermaLink="false">http://richardbushnell.net/index.php/2008/01/08/are-you-keeping-up-with-software-development/</guid>
		<description><![CDATA[DotNetRocks, the internet talkshow for .Net developers, has a great show about development changes over the past 20 years. Carl Franklin interviews Kathleen Dollard, who has put a list of changes together, and is publishing them online. If you want to know where you currently stand in your software development, you want to listen to [...]]]></description>
			<content:encoded><![CDATA[
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fare-you-keeping-up-with-software-development%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<br><p><a href="http://www.dotnetrocks.com/default.aspx" target="_blank">DotNetRocks</a>, the internet talkshow for .Net developers, has a great <a href="http://www.dotnetrocks.com/default.aspx?showNum=304" target="_blank">show about development changes</a> over the past 20 years. <a href="http://www.intellectualhedonism.com/2007/12/27/NETRocks302JeffProsiseGoesDeepOnSilverlight.aspx" target="_blank">Carl Franklin</a> interviews <a href="http://msmvps.com/blogs/kathleen/default.aspx" target="_blank">Kathleen Dollard</a>, who has put a <a href="http://msmvps.com/blogs/kathleen/archive/2007/11/14/looking-at-the-list-1-of-6-or-7.aspx" target="_blank">list of changes together</a>, and is publishing them online.</p>
<p>If you want to know where you currently stand in your software development, you want to listen to this show. You might even want to take notes.</p>
<p>If you are recruiting developers, you also should listen to this show. You could use very effectively it to find out what kind of people you are interviewing. </p>
<p>The first 10 of 60 items on Kathleen&#8217;s list of changes are:</p>
<ol>
<li>Parallel entities</li>
<li>N-Tier</li>
<li>Sheer magnitude</li>
<li>Application code generation</li>
<li>SOA (Service Oriented Architecture)</li>
<li>Semantics and canonical messages</li>
<li>Workflow</li>
<li>Rules engines</li>
<li>Aspect oriented programming</li>
<li>Impact of libraries</li>
</ol>
<p>Every software developer or architect should know at least a little about each item on this list. If you don&#8217;t, maybe you could use this to pull your career up a level.</p>

<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.richardbushnell.net%2F2008%2F01%2F08%2Fare-you-keeping-up-with-software-development%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="false" style="border:none; overflow:; width:450px; height:30px"></iframe>
<!-- using Like-Button-Plugin-For-Wordpress [v4.2] | by http://www.gb-world.net -->
]]></content:encoded>
			<wfw:commentRss>http://www.richardbushnell.net/2008/01/08/are-you-keeping-up-with-software-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

