<?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>willcodeforcoffee.com &#187; .NET</title>
	<atom:link href="http://willcodeforcoffee.com/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://willcodeforcoffee.com</link>
	<description>The personal programming blog of Eric Hoff.</description>
	<lastBuildDate>Tue, 02 Mar 2010 17:06:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Oh Unit Tests? Wherefore Art Thou, Unit Tests?</title>
		<link>http://willcodeforcoffee.com/2010/02/26/oh-unit-tests-wherefore-art-thou-unit-tests/</link>
		<comments>http://willcodeforcoffee.com/2010/02/26/oh-unit-tests-wherefore-art-thou-unit-tests/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 18:07:05 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[My Two Cents]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/?p=119</guid>
		<description><![CDATA[The first red flag was that this was a Visual Studio 2005 project and I was using Visual Studio 2008; the second was the five projects that couldn't be found in VSS - each one ending with .Test...]]></description>
			<content:encoded><![CDATA[<p>There are few things that help a developer maintain an application better than Unit Tests. There are a few things that make me nervous when I open a solution from VSS, but nothing makes me more nervous than a lack of unit tests projects.</p>
<p>I know the Unit Testing drum has been beaten for years, so instead of getting on a soap box and berating the Internet for not Unit Testing, I&#8217;m going to give you a story about a recent project I worked on, and how unit testing helped me succeed.</p>
<p><span id="more-119"></span>It all started last fall when I started work at a new client&#8217;s office. I opened the project from VSS and red flags started popping up. The first red flag was that this was a Visual Studio 2005 project and I was using Visual Studio 2008 (they did not install VS2005 on my computer), but this wasn&#8217;t really a problem &#8211; I was <em>assured </em>that they had standardized on Visual Studio 2008 now and the .NET Framework 3.5. The second red flag was the five projects in the Solution that couldn&#8217;t be found in VSS &#8211; each one ending with .Tests. I still don&#8217;t know why they were removed &#8211; but my guess is that they were removed because they didn&#8217;t pass.</p>
<p>The first thing I did was create a new Unit Test project. After examining source code thoroughly I created a test harness of unit tests of the existing functionality of the project. This is a very difficult and risky task since these tests are built under the assumption the current code works correctly! This is <strong>not</strong> a safe assumption when projects are missing from the solution. However, the fewer behavior changes I introduced to a program already in production use the better.</p>
<p>During this period (probably one week) I uncovered and fixed many bugs. I re-factored a lot of code. I did more testing.</p>
<p>Then I added my new functionality, retested and it worked.</p>
<p>I added more tests. They went from red to green.</p>
<p>My confidence grew with each little green light.</p>
<p>I added integration tests to the unit tests. These all passed.</p>
<p>Eventually the program was ready for deployment to the testing server (UAT &#8211; User Acceptance Testing) &#8211; a model of the production environment. And I was confident! My tests were passing!</p>
<p>This is when everything went wrong.</p>
<p>The Problem? Updating to the .NET Framework 3.5. Of course I had been misled when they assured me that they standardized on 3.5 &#8211; the production environment was still very much in 2.0 (and I was <em>strongly</em> assured they would not upgrade &#8211; even though I hadn&#8217;t asked).</p>
<p>The Solution? Unit Testing! I had extensively used the new productivity features of 3.5 like LINQ and Extension Methods. It took some rewriting and work but I was able to revert and rewrite everything back to 2.0 compatibility in one afternoon! The <strong>only </strong>reason I could do it so quickly was the unit tests I wrote that told me the functionality was still there and working in the old Framework. It took more time to find another computer with Visual Studio 2005 and the .NET Framework 2.0 installed (and not the 3.5 installed at all) than it took to rewrite the app to the older framework version.</p>
<p>There were more problems that were solved with more testing, but Unit Tests saved me a lot of time and guessing. There were some other things that could have made the project more successful such as better communication with other developers and the production team. Those issues were beyond my control. But I was so glad I had those unit tests, they saved my bacon. They gave me confidence in the completed software. The Unit Tests were also something I could show my client, the maintenance teams, and the testing teams in terms of functionality assurance for them.</p>
<p>Ah success! A new project! Another Solution! &#8230;and guess what? No unit tests. *sigh* Here we go again! <img src='http://willcodeforcoffee.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2010/02/26/oh-unit-tests-wherefore-art-thou-unit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What Makes A Good ORM?</title>
		<link>http://willcodeforcoffee.com/2009/09/21/what-makes-a-good-orm/</link>
		<comments>http://willcodeforcoffee.com/2009/09/21/what-makes-a-good-orm/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 20:31:27 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Reviews]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/?p=106</guid>
		<description><![CDATA[I&#8217;ve been using a few ORMs with a few projects, including NHibernate, SubSonic, Linq-to-SQL (L2S) and Entity Framework (EF) and even Strongly-Typed DataSets. Each provides an abstraction from the database, a few have Visual Studio integration, and they all simplify data storage for you, but I&#8217;ve found some are definitely better than others. XML Files [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using a few ORMs with a few projects, including NHibernate, SubSonic, Linq-to-SQL (L2S) and Entity Framework (EF) and even Strongly-Typed DataSets. Each provides an abstraction from the database, a few have Visual Studio integration, and they all simplify data storage for you, but I&#8217;ve found some are definitely better than others.</p>
<p><span id="more-106"></span><strong>XML Files</strong></p>
<p>Most of the ORMs I&#8217;ve used required a lot of configuration in XML files. Probably the worst offender in this sense is NHibernate. L2S, EF and DataSets are bad too, but at least they have tooling in Visual Studio to make it easier to use and hide the XML from you. In this respect, the Fluent NHibernate project is a life-saver for anyone using NHibernate.</p>
<p><strong>POCOs and the Repository Design Pattern<br />
</strong></p>
<p>Another thing most ORMs provide you with is an ability to use simple POCOs or <span style="text-decoration: underline;">P</span>lain <span style="text-decoration: underline;">O</span>ld <span style="text-decoration: underline;">C</span>LR (or <span style="text-decoration: underline;">C</span>#) <span style="text-decoration: underline;">O</span>bjects. Ordinary DataTables and Strongly-Typed DataSets come with a lot of extra &#8220;clutter&#8221; to them, this is especially visible when you&#8217;re interacting with them using IntelliSense inside Visual Studio. The clutter appears as all the baggage that comes along with DataTables, as well as the fact you have to use DataAdapters, and fetch row objects (i.e. UserTableRow).</p>
<p>It is really nice to just have a simple class that can do what you need it to within the &#8220;domain&#8221; (following the Domain Driven Design ideas). It simplifies your data layer model significantly, and of course reduces a lot of clutter.</p>
<p>NHibernate and SubSonic are the clear leaders here, NHibernate even more so, but SubSonic can regenerate mappings after you&#8217;ve made changes to your database.</p>
<p><strong>Simple Database Schema Creation</strong></p>
<p>Another thing I like to see in an ORM tool is the ability to take your data-layer mappings and generate a database schema from that. The primary reason is that database versioning during development is <em>hard</em>. Keeping everyone on the same page with databases is difficult and making each developer use the <em>same</em> database is hard to impossible. If your ORM can just create and/or update your database schema for you off the bat, keeping each developer up-to-date is a breeze. No more &#8220;why is my app crashing?&#8221;, &#8220;where is that SQL script?&#8221;, &#8220;who did this?&#8221; shouting over the cubicle walls, instead you just run the update scripts and you&#8217;re all set.</p>
<p>As far as I know there is only one <em>clear </em>winner here and that is NHibernate; L2S, EF and Strongly-Typed DataSets don&#8217;t have this functionality at all. I believe that the 3.x version of SubSonic does have this now, and version 2.x had a version of migrations (that may be obsolete in version 3).</p>
<p><strong>My Pick for Best ORM</strong></p>
<p>I&#8217;ve personally switched from the SubSonic camp to the NHibernate camp in the last year. With Fluent NHibernate and guidance from the <a href="http://sharparchitecture.net/" target="_blank">S#arpArchitecture</a> projects all real difficult barriers into NHibernate have fallen. SubSonic is an awesome ORM and does help you get up to speed quickly, but it does feel like it isn&#8217;t developer or maintained quite as much as NHibernate is, and the differences between 2.x and 3.x versions was so great as to almost be intimidating to me (almost)!</p>
<p>Entity Framework is good, but I&#8217;ve had problems with the tooling and updating the mapping schema in the projects I&#8217;ve used it in. To me, there is very little programmatic difference with Linq-to-SQL, in fact EF seems to have more functionality. According to what I&#8217;ve read, it <em>will</em> have Database Schema Generation in the .NET 4.0 version, but NHibernate 2.1 already has LINQ support (via another library) so EF is really still just playing catch-up.</p>
<p>Strongly-Typed DataSets are pretty much an obsolete technology as far as I&#8217;m concerned &#8211; from the .NET 1.0/1.1 days. They do provide some simplification over bare metal ADO.NET, but I don&#8217;t find them as easy to use as NHibernate or any of the other ORMs I&#8217;ve discussed so I&#8217;ve seen little benefit in modern projects.</p>
<p><strong>Summary</strong></p>
<p>While I know there are more options out there than just the ones I&#8217;ve pointed out, NHibernate is the tool that <em>best</em> represents what I&#8217;m looking for in an ORM.</p>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2009/09/21/what-makes-a-good-orm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web Services, Overhead and Performance</title>
		<link>http://willcodeforcoffee.com/2009/05/30/web-services-overhead-and-performance/</link>
		<comments>http://willcodeforcoffee.com/2009/05/30/web-services-overhead-and-performance/#comments</comments>
		<pubDate>Sat, 30 May 2009 14:28:42 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/?p=89</guid>
		<description><![CDATA[A Webservice denotes a logical boundary within your software between applications and computers.]]></description>
			<content:encoded><![CDATA[<p>Earlier this week a friend of mine asked me for some advice over an argument that some developers at his company were having.  They are collecting data from a device, and one developer wanted to have the device send the data to a web service to receive the data.  The other developer thought a web service would be to much &#8220;overhead&#8221; and wanted a different solution using direct writes or XML to the database and a UDP socket notifier for the server to parse the data.</p>
<p><span id="more-89"></span>I didn&#8217;t actually speak to the developers myself, but my friend sent me an MSN transcript of their &#8220;discussion&#8221;.  Apparently tempers grew quite hot!  I did read most of the discussion and spoke with my friend to learn more about the system.</p>
<p>It turns out there would be many transactions with the database and web service coming from these devices with a fair bit of geographic data that had to be processed at the server.  One developer felt that using a web service would  involve too much XML/SOAP overhead and wanted to write his data as XML to the database directly.</p>
<p>I was not in favor of this suggestion.  A web service makes perfect sense in a .NET situation.  It denotes a logical boundary between applications and computers, and would be pretty obvious to any supporting developer what is going on.  Usually two applications do not share the same database (maybe the same server, but not the same database).   This increases complexity.</p>
<p>I was also not convinced that the &#8220;overhead&#8221; of the SOAP would actually limit the application.  Microsoft has done a lot to optimize web services.  They&#8217;re easy to develop now, easy to consume, and as I said: a logical boundary between systems.</p>
<p>In the end I strongly recommended that they do some benchmarking.  This early in the game is the perfect time to test assumptions.  I also recommended using one of the new Service Bus APIs out there that use technologies like Microsoft Message Queues if the web services are too slow.</p>
<p>Also, as Jeff Atwood recommended in his Coding Horror blog, sometimes it is cheaper to throw another $5000 server at the solution (even TWO servers) instead of paying two $70,000 / year developers to spend three-to-six months doing performance enhancements.</p>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2009/05/30/web-services-overhead-and-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NHibernate vs. SubSonic Another Look</title>
		<link>http://willcodeforcoffee.com/2009/01/22/nhibernate-vs-subsonic-another-look/</link>
		<comments>http://willcodeforcoffee.com/2009/01/22/nhibernate-vs-subsonic-another-look/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 21:26:05 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/?p=68</guid>
		<description><![CDATA[Earlier I had compared NHibernate and SubSonic and chose SubSonic because it had migrations.  I&#8217;ve done two projects, one using SubSonic and the other using NHibernate and I can now compare the features a little better. NHibernate is documented better than I originally thought if you look at nhforge.com, not at the hibernate site.  It [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier I had compared NHibernate and SubSonic and chose SubSonic because it had migrations.  I&#8217;ve done two projects, one using SubSonic and the other using NHibernate and I can now compare the features a little better.</p>
<p>NHibernate is documented better than I originally thought if you look at nhforge.com, not at the hibernate site.  It also does some nice schema generation based on your mapping.  You still have to go through &#8220;XML Hell&#8221; to map the site, unless you use another mapper or FluentNHibernate (which does the mapping for you in code) or another tool.  As an ORM SubSonic feals cleaner and quicker since it generates the ORM classes from the database, where-as NHibernate generates the Schema from your mappings and ORM classes.</p>
<p><span id="more-68"></span></p>
<p>Generating the schema in NHibernate, even with Fluent-NHibernate, seems to take more work than generating the classes in SubSonic took.  However, it&#8217;s still quite simple.</p>
<p>Better support for Oracle was one of the primary drivers for me using NHibernate for this project, but after making the switch I&#8217;ve found a lot more interesting stuff, including an exciting ecosystem of NHibernate addins, plugins and updates.  I think it really appeals to the gadget geek in me.</p>
<p>The other thing that kept me thinking about NHibernate is the sheer number of other blogger / developers out there are using NHibernate!  After I started looking I was surprised to see how many blogging developers actually used NHibernate for their projects.  So not only do I feel like I&#8217;m in good company right now, but there are also a lot of people out there with good advice on best practices.</p>
<p>So, I&#8217;m taking another look at NHibernate right now, and I might post on it again later!</p>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2009/01/22/nhibernate-vs-subsonic-another-look/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparing Open Source ORM: SubSonic vs. NHibernate</title>
		<link>http://willcodeforcoffee.com/2008/10/09/comparing-open-source-orm-subsonic-vs-nhibernate/</link>
		<comments>http://willcodeforcoffee.com/2008/10/09/comparing-open-source-orm-subsonic-vs-nhibernate/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 18:37:27 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/?p=40</guid>
		<description><![CDATA[ORM tools are probably one of the most time-saving tools for a web developer.  Nothing is more tedious than building a data access layer, adding caching or coordinating transactions.  Well okay, sometimes things like that are really fun, but when you&#8217;re trying to start a project out from scratch you want to get something up [...]]]></description>
			<content:encoded><![CDATA[<p>ORM tools are probably one of the most time-saving tools for a web developer.  Nothing is more tedious than building a data access layer, adding caching or coordinating transactions.  Well okay, sometimes things like that are really fun, but when you&#8217;re trying to start a project out from scratch you want to get something up and running ASAP to show to your customers.</p>
<p>In the .NET world there are two powerful ORM tools out there, <a title="http://www.hibernate.org/" href="http://www.hibernate.org/" target="_blank">NHibernate</a> and <a title="http://subsonicproject.com/" href="http://subsonicproject.com/" target="_blank">SubSonic</a>.  NHibernate is based on the successful and popular Java EE ORM Hibernate.  SubSonic is an entirely new ORM developed in the Ruby on Rails ActiveRecord development model, with some nice .NET 2.0 features like Controllers that you can use with ObjectDataSource controls.</p>
<p>For a recent project I quickly evaluated both software tools, and I&#8217;m publishing what I learned here.</p>
<p><span id="more-40"></span></p>
<p><strong>Setting up NHibernate:</strong></p>
<p>The NHibernate Object/Relational Mapping is setup using XML files.  If you&#8217;re using the 3.5 Framework you can use a fluent interface instead (i.e. code the mapping in C# instead of XML).  I didn&#8217;t find a tool that generated the XML or interface automatically for you, but there must be one out there.</p>
<p>Changes to the database require changing the mappings, which means digging through XML.</p>
<p><strong>Setting up SubSonic:</strong></p>
<p>SubSonic mappings are generated via an external tool called SubCommander run from the tools menu.  Any changes to the database require regenerating these mapped classes.  SubCommander connects to your database, reads the Schema and generates O/R classes.  The O/R classes are Partial classes in C# so that you can create methods for the classes that won&#8217;t get overwritten by the tool each time you write it.</p>
<p>Changes to the database require re-running SubCommander (which can be done via the IDE).</p>
<p>The SubSonic team has also come up with some great videos to help you get started.</p>
<p><strong>Migrations</strong></p>
<p>The big thing that sold me on SubSonic was the new <a href="http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/" target="_blank">Migrations</a> feature.  It uses a Ruby on Rails style migrations tool to help keep your database versioned.  It&#8217;s really excellent for a first generation tool.  It&#8217;s missing a few things, like it&#8217;s hard to set Unique Keys, but otherwise it is excellent.  This is also the first .NET Migrations utility I&#8217;ve found.</p>
<p>Migrations are one of the easiest ways to keep your database versioned, which has always been a problem for me when updating Test and Production databases.</p>
<p><strong>Conclusion</strong></p>
<p>I ended up selecting SubSonic for the following reasons:</p>
<ol>
<li>No XML configuration files (running a tool is much easier, IMHO)</li>
<li>Migrations</li>
<li>Their excellent video tutorials and directions on their website</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2008/10/09/comparing-open-source-orm-subsonic-vs-nhibernate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Open / Closed Principle</title>
		<link>http://willcodeforcoffee.com/2008/06/25/the-open-closed-principle/</link>
		<comments>http://willcodeforcoffee.com/2008/06/25/the-open-closed-principle/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 15:41:18 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/2008/06/25/the-open-closed-principle/</guid>
		<description><![CDATA[I just read a great article in MSDN Magazine (available online here) on the Open / Closed Principle from the Patterns in Practice column.&#160; I really enjoyed it.&#160; I&#8217;m going to have to spend some more time learning some more design patterns.&#160; I&#8217;ve spent some time learning Model-View-Presenter (MVP) and Inversion of Control (IoC) but [...]]]></description>
			<content:encoded><![CDATA[<p>I just read a great article in MSDN Magazine (available online <a href="http://msdn.microsoft.com/en-ca/magazine/cc546578.aspx" target="_blank">here</a>) on the Open / Closed Principle from the Patterns in Practice column.&#160; I really enjoyed it.&#160; I&#8217;m going to have to spend some more time learning some more design patterns.&#160; I&#8217;ve spent some time learning Model-View-Presenter (MVP) and Inversion of Control (IoC) but haven&#8217;t put either into practice yet.&#160; Who knows what the future holds though?</p>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2008/06/25/the-open-closed-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using var And Living With The Consequences</title>
		<link>http://willcodeforcoffee.com/2008/06/20/using-var-and-living-with-the-consequences/</link>
		<comments>http://willcodeforcoffee.com/2008/06/20/using-var-and-living-with-the-consequences/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 23:06:15 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# 3.5 Features]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/?p=28</guid>
		<description><![CDATA[Another new feature added in C# 3.5 to accommodate anonymous types is the introduction of the var type keyword.&#160; In his Coding Horror blog Jeff Atwood is extolling the virtues of using var to reduce type declaration redundancy.&#160; His example is converting: StringBuilder sb = new StringBuilder(256); UTF8Encoding e = new UTF8Encoding(); MD5CryptoServiceProvider md5 = [...]]]></description>
			<content:encoded><![CDATA[<p>Another new feature added in C# 3.5 to accommodate anonymous types is the introduction of the <em><a href="http://msdn.microsoft.com/en-us/library/bb383973.aspx" target="_blank">var</a></em> type keyword.&#160; In his <a href="http://www.codinghorror.com/blog/archives/001136.html" target="_blank">Coding Horror</a> blog Jeff Atwood is extolling the virtues of using var to reduce type declaration redundancy.&#160; His example is converting:</p>
</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ed0cf444-796c-435c-bb51-002172817e68" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">StringBuilder sb </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> StringBuilder(</span><span style="color: #800080;">256</span><span style="color: #000000;">);
UTF8Encoding e </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> UTF8Encoding();
MD5CryptoServiceProvider md5 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> MD5CryptoServiceProvider();
</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>into this:</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:753422c6-a429-4bd1-a415-abda65ace6ca" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">var sb </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> StringBuilder(</span><span style="color: #800080;">256</span><span style="color: #000000;">);
var e </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> UTF8Encoding();
var md5 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> MD5CryptoServiceProvider();
</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>His point is that declaring the type as StringBuilder and then <u>assigning it a new StringBuilder</u> is redundant &#8211; StringBuilder appears twice on that line of code, before and after the assignment operator (=).&#160; He makes an excellent point because the example using var is far more concise than the original example.&#160; Furthermore, we should be more interested in what is being assigned than what we&#8217;re assigning too (at least on that line of code).&#160; I find when I read the first block my eyes stick on the left-hand side of the assignment, but in the second example I read the right-hand side code instead.</p>
<p>As a side note. the excellent tool <a href="http://www.jetbrains.com/resharper/index.html" target="_blank">ReSharper 4.0</a> suggests using var for <em>every</em> local variable declaration by default.&#160; It&#8217;s one of the first things I changed in ReSharper when I started using it.</p>
<p>You see, I don&#8217;t entirely agree with using var.&#160; Using var is fine, but we aren&#8217;t converting C# into a dynamically typed language like Python or JavaScript &#8211; C# is still statically typed, using var just forces the compiler to figure out the type you&#8217;re using, usually based on the first value assigned to it.&#160; In Python or JavaScript I could take sb from the above example and assign to it the UTF8Encoding() object reference on the next line, but C# won&#8217;t let you do that, the compiler will give you an error.</p>
<p>I don&#8217;t like the illusion, so where possible I will use var, but I&#8217;ll keep using types too when necessary for clarity.</p>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2008/06/20/using-var-and-living-with-the-consequences/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding Common Values in Two Lists in C# Using LINQ</title>
		<link>http://willcodeforcoffee.com/2008/06/19/finding-common-values-in-two-lists-in-c-using-linq/</link>
		<comments>http://willcodeforcoffee.com/2008/06/19/finding-common-values-in-two-lists-in-c-using-linq/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 16:46:59 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://willcodeforcoffee.com/?p=26</guid>
		<description><![CDATA[Finding common values in two lists is simple in .NET 3.5 thanks to LINQ!&#160; I found this example here and here. In practice here is what I did: var currentOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(this.OperatorId) select fac.ID; var deleteOperatorFacilityIds = from fac in Facility.Facility.ListAllForOperator(operatorId) select fac.ID; var commonFacilityIds = currentOperatorFacilityIds.Intersect(deleteOperatorFacilityIds);]]></description>
			<content:encoded><![CDATA[<p>Finding common values in two lists is <em>simple</em> in .NET 3.5 thanks to LINQ!&#160; I found this example <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#intersect1" target="_blank">here</a> and <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#intersect2" target="_blank">here</a>.</p>
<p>In practice here is what I did:</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3754ea51-ffd2-44e6-882e-7a0c6d200ac1" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; width: 523px; padding-top: 0px">
<pre style="background-color:White;;overflow: none;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">var currentOperatorFacilityIds </span><span style="color: #000000;">=</span><span style="color: #000000;"> from fac </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> Facility.Facility.ListAllForOperator(</span><span style="color: #0000FF;">this</span><span style="color: #000000;">.OperatorId) select fac.ID;
var deleteOperatorFacilityIds </span><span style="color: #000000;">=</span><span style="color: #000000;"> from fac </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> Facility.Facility.ListAllForOperator(operatorId) select fac.ID;

var commonFacilityIds </span><span style="color: #000000;">=</span><span style="color: #000000;"> currentOperatorFacilityIds.Intersect(deleteOperatorFacilityIds);
</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Really simple!&#160; I <em>love</em> LINQ.</p>
]]></content:encoded>
			<wfw:commentRss>http://willcodeforcoffee.com/2008/06/19/finding-common-values-in-two-lists-in-c-using-linq/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
