<?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>Cameron McKay &#187; c#</title>
	<atom:link href="http://cdmckay.org/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Tue, 11 Oct 2011 05:05:09 +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>Anders Hejlsberg talks about C# 4.0</title>
		<link>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/</link>
		<comments>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 01:46:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net 4.0]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1365</guid>
		<description><![CDATA[Channel 9 has posted a great talk by Anders Hejlsberg, the original author of Turbo Pascal, the chief architect of Delphi, and the lead architect of C#. History lesson: Anders was lured away from Borland by Microsoft with a hefty offer of a $1.5 million signing bonus, a base salary of up to $200,000, and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://channel9.msdn.com/posts/matthijs/C-40-and-beyond-by-Anders-Hejlsberg/">Channel 9 has posted a great talk</a> by <a href="http://en.wikipedia.org/wiki/Anders_Hejlsberg">Anders Hejlsberg</a>, the original author of Turbo Pascal, the chief architect of Delphi, and the lead architect of C#.</p>
<p>History lesson: Anders was <a href="http://news.cnet.com/2009-1023-229218.html">lured away from Borland by Microsoft</a> with a hefty offer of a $1.5 million signing bonus, a base salary of up to $200,000, and options to buy 75,000 shares of Microsoft stock.</p></blockquote>
<p>Looks like it was worth it, as each C# release has added many interesting and powerful features, while maintaining a fine balance between complexity and simplicity.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/18/anders-hejlsberg-talks-about-c-4-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Inconsistencies in the .NET Enum class</title>
		<link>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/</link>
		<comments>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 05:59:44 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Dnum]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[enum]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=948</guid>
		<description><![CDATA[During a recent project of mine, I had to do a lot of enum manipulation using the .NET Enum class and I have to say I wasn&#8217;t impressed. Besides the shortcoming of it not being a generic class (and thus not being especially type-safe), Enum also has some strange inconsistencies in terms of how it [...]]]></description>
			<content:encoded><![CDATA[<p>During a recent project of mine, I had to do a lot of enum manipulation using the .NET Enum class and I have to say I wasn&#8217;t impressed.  Besides the shortcoming of it not being a generic class (and thus not being especially type-safe), Enum also has some strange inconsistencies in terms of how it handles matched integral types.</p>
<p><span id="more-948"></span></p>
<h3>GetName</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx">GetName</a></code> return null for any integral type you pass it except those that correspond to a constant?</p>
<p>For example, this is completely valid:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">byte</span> <span style="color: #008000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
var fruit <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetName</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #6666cc; font-weight: bold;">ulong</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// fruit now equals null</span></pre></div></div>

<p>In every other case in the Enum class where you pass an integral value outside the range of the underlying type, you get an exception.  Except here.</p>
<h3>IsDefined</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.isdefined.aspx">IsDefined</a></code> throw an exception when you pass it an integral value that can be safely narrowed to the enumeration&#8217;s underlying type?</p>
<p>For example, this will throw an exception:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">byte</span> <span style="color: #008000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
var defined <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsDefined</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Why?  Because it&#8217;s a integer literal (and therefore type <code>int</code>) and the underlying type of <code>Fruit</code> is <code>byte</code>.  This would be alright if this is how the Enum class consistently treated this case, but it&#8217;s not.  In every other method, the Enum class is OK with you passing in an <code>int</code> value for a <code>byte</code> enumeration (as long as it&#8217;s within the range of the underlying type).</p>
<h3>ToObject</h3>
<p>Why does <code><a href="http://msdn.microsoft.com/en-us/library/system.enum.toobject.aspx">ToObject</a></code> silently overflow for values outside the range of the enumeration&#8217;s underlying type?</p>
<p>For example, this code will give the following non-intuitive result:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">enum</span> Fruit <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">byte</span> <span style="color: #008000;">&#123;</span> Apple, Orange, Cumquat <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
var apple <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToObject</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var elppa <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">Enum</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToObject</span><span style="color: #008000;">&#40;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>Fruit<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">256</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span>apple, elppa<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Assuming that we ignore the fact that silently overflowing is typically a bad idea, this is again completely inconsistent with the rest of the library.  Why overflow here, but not in <code>GetName</code>?</p>
<h3>Conclusion</h3>
<p>The .NET Enum class is not lacking in it&#8217;s ability to extract any information you need from an enumeration.  It is, however, a little inconsistent in terms of how it treats underlying type mismatches, overflows and narrowing conversions.  Barring a new, generified Enum class from Microsoft, the best way to deal with these inconsistencies is to either be aware of them, or use a third-party library to hide them.</p>
<p><em>(Edit: Some people have noted that my conclusion was a bit weak and preachy towards Dnum, which I have to admit is not without truth.  I&#8217;ve seen edited it.  For the record, the old conclusion was: &#8220;Fortunately, these and other problems have been fixed in my <a href="http://cdmckay.org/blog/dnum/">Dnum</a> library, which should be released in the next couple days.  Be sure to check it out.&#8221;).</em></p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/09/14/inconsistencies-in-the-net-enum-class/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to put spaces in your NHibernate enums</title>
		<link>http://cdmckay.org/blog/2009/07/12/how-to-put-spaces-in-your-nhibernate-enums/</link>
		<comments>http://cdmckay.org/blog/2009/07/12/how-to-put-spaces-in-your-nhibernate-enums/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 02:33:25 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=770</guid>
		<description><![CDATA[NHibernate lets you map C# enums to database columns. This can be quite convenient in cases where you have a database column for something like gender. To map them, you follow the procedure outlined here. That works all fine and dandy, as long as the strings you want to store in the database are valid [...]]]></description>
			<content:encoded><![CDATA[<p>NHibernate lets you map C# enums to database columns.  This can be quite convenient in cases where you have a database column for something like gender.  To map them, you follow the procedure outlined <a href="http://codebetter.com/blogs/jeremy.miller/archive/2006/02/20/138732.aspx">here</a>.</p>
<p>That works all fine and dandy, as long as the strings you want to store in the database are valid C# identifiers.</p>
<p><span id="more-770"></span></p>
<p>What do I mean?  Well, consider an enum of something like shipping countries.  Imagine we&#8217;re only shipping to Canada and the USA.  That&#8217;s easy enough:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Country.cs</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> Country
<span style="color: #008000;">&#123;</span>
  Canada,
  USA
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now imagine that after we write that code, we find out the customer hates <a href="http://en.wikipedia.org/wiki/Three-letter_acronym">TLA</a>s and wants the database entry to contain &#8220;United States of America&#8221;.  When we go to the enum to change it, we realize we&#8217;re screwed.  You can&#8217;t have spaces in a C# identifier!</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Country.cs</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> Country
<span style="color: #008000;">&#123;</span>
  Canada,
  United States of America <span style="color: #008080; font-style: italic;">// Compile error!</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Fortunately, the ever-flexible NHibernate can handle this case using the procedure outlined <a href="http://softwareblog.morlok.net/2009/07/02/mapping-enums-to-custom-strings-in-nhibernate/">here</a>.</p>
<p>Let&#8217;s apply this procedure to the shipping locations example.  Recall that C# was not happy with us putting spaces in our enum members.  How can we make that code compile?  Well, an underscore is a valid part of a C# identifier, and it&#8217;s kinda like a space, so let&#8217;s try that:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Country.cs</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> Country
<span style="color: #008000;">&#123;</span>
  Canada,
  United_States_of_America <span style="color: #008080; font-style: italic;">// C# is happy!</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Next, we need to derive our <code>EnumStringType</code> class so that NHibernate will store the string value of the enum instead of the integer value:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// CountryEnumStringType.cs</span>
<span style="color: #6666cc; font-weight: bold;">class</span> CountryEnumStringType <span style="color: #008000;">:</span> NHibernate<span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EnumStringType</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// The 30 is the size of the field in the DB.</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> CountryEnumStringType<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>Country<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">30</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Cool, cool, so far so good.  This is about how much we&#8217;d need to do if we had no spaces to worry about.  Unfortunately, if we used these two classes we made as is, the database would contain &#8220;United_States_of_America&#8221; instead of &#8220;United States of America&#8221;.  And that&#8217;s no good&#8230;  so how do we get around that?</p>
<p>Why, we need to override the <code>GetValue</code> and <code>GetInstance</code> methods, of course!</p>
<p>Let&#8217;s <a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/">plunk open the NHibernate source code</a> and see what they have there now, to use as a guide.  Ahh, <a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Type/EnumStringType.cs">there she is</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
&nbsp;
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetInstance<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> code<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">//code is an named constants defined for the enumeration.</span>
  <span style="color: #0600FF; font-weight: bold;">try</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> StringToObject<span style="color: #008000;">&#40;</span>code <span style="color: #0600FF; font-weight: bold;">as</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>ArgumentException ae<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> HibernateException<span style="color: #008000;">&#40;</span>
      <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Can't Parse {0} as {1}&quot;</span>,
      code, ReturnedClass<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span>, ae<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///</span>
&nbsp;
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetValue<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> code<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">//code is an enum instance.</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> code <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">?</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span> <span style="color: #008000;">:</span> code<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The comments aren&#8217;t the greatest, but we can figure out what&#8217;s going on.  <code>GetInstance</code> converts a string in an enum instance, and <code>GetValue</code> converts an enum to a string.</p>
<p>With that in mind, let&#8217;s get to work.  First, let&#8217;s put in some stubs in our <code>CountryEnumStringType</code> class:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// CountryEnumStringType.cs</span>
<span style="color: #6666cc; font-weight: bold;">class</span> CountryEnumStringType <span style="color: #008000;">:</span> NHibernate<span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span><span style="color: #008000;">.</span><span style="color: #0000FF;">EnumStringType</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// The 30 is the size of the field in the DB.</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> CountryEnumStringType<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span> <span style="color: #008000;">&#40;</span>Location<span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">30</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #008080; font-style: italic;">/// Converts a string to an enum instance.</span>
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetInstance<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> code<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #008080; font-style: italic;">/// Converts a enum instance to a string.</span>
  <span style="color: #008080; font-style: italic;">///</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetValue<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> code<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Sweet.  Now, our basic strategy here is this:</p>
<ul>
<li>when NHibernate asks for an enum instance (and passes a string), we want to replace spaces in the string with underscores, and then find the corresponding enum instance.</li>
<li>when NHibernate asks for a string (and passes an enum instance), we want to convert the enum to a string, and then replace all underscores with spaces.</li>
</ul>
<p>First, we modify <code>GetInstance</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Converts a string to an enum instance.</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetInstance<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> code<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// This is the string coming from the database, so it will</span>
  <span style="color: #008080; font-style: italic;">// have spaces in it that we need to turn into underscores.</span>
  var str <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span> code<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// If the string is null, just pass it on, no need to replace</span>
  <span style="color: #008080; font-style: italic;">// spaces with underscores.</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> StringToObject<span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">return</span> StringToObject<span style="color: #008000;">&#40;</span>str<span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, <span style="color: #666666;">&quot;_&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In case you&#8217;re wondering where the hell <code>StringToObject</code> came from, and what it does, I can tell you this:</p>
<ul>
<li>It was used in the code for the <code>EnumStringType</code> class,</li>
<li>its name implies that it&#8217;s used to convert a string into an object,</li>
<li>the context in which it is used supports the previous implication, and</li>
<li>if you really want to know what it does, read the code for the <code>AbstractEnumType</code> <a href="https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Type/AbstractEnumType.cs">base class</a>.</li>
</ul>
<p>With that out of the way, let&#8217;s move on to writing the code for the <code>GetValue</code> method.  In this case we&#8217;re looking at converting an enum instance into a string (that will eventually be stored in the database):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Converts a enum instance to a string.</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">object</span> GetValue<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> code<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> code <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span>
    <span style="color: #008000;">?</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span>
    <span style="color: #008000;">:</span> code<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'_'</span>, <span style="color: #666666;">' '</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The <code>GetValue</code> is pretty straight forward.  We basically just tagged on the <code>Replace</code> method to replace all underscores with spaces.</p>
<p>That should about do it.  To close, I&#8217;ll give a sample usage of it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Order.cs</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Order
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">long</span> Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> Country Country <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Then this would be the mapping:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Order.hbm.xml --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Order&quot;</span> <span style="color: #000066;">table</span>=<span style="color: #ff0000;">&quot;TblOrder&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Country&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;WebApp.Domain.CountryEnumStringType, WebApp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/12/how-to-put-spaces-in-your-nhibernate-enums/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Joshua Bloch&#8217;s Builder Pattern in C#</title>
		<link>http://cdmckay.org/blog/2009/07/03/joshua-blochs-builder-pattern-in-csharp/</link>
		<comments>http://cdmckay.org/blog/2009/07/03/joshua-blochs-builder-pattern-in-csharp/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 22:02:49 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[design-patterns]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=610</guid>
		<description><![CDATA[Having spent a lot of time programming in Java over the last two years, I&#8217;ve made heavy use of Joshua Bloch&#8216;s Java Builder pattern (also Effective Java Item&#160;2). Recently, I&#8217;ve started a fairly large project in C# 3.0. As it happens, there came a point where I wanted to use a pattern similar to Bloch&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Having spent a lot of time programming in Java over the last two years, I&#8217;ve made heavy use of <a href="http://en.wikipedia.org/wiki/Joshua_Bloch">Joshua Bloch</a>&#8216;s Java <a href="http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-2689.pdf">Builder pattern</a> (also <a href="http://java.sun.com/docs/books/effective/">Effective Java</a> Item&nbsp;2).</p>
<p>Recently, I&#8217;ve started a fairly large project in C# 3.0.  As it happens, there came a point where I wanted to use a pattern similar to Bloch&#8217;s Builder.  However, as anyone who tries to use this pattern in C# will quickly find out, Bloch&#8217;s Builder doesn&#8217;t translate perfectly to C#.  </p>
<p><span id="more-610"></span></p>
<p>Here is how Bloch&#8217;s Builder looks in C# (with changes made only for syntax):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> servingSize, <span style="color: #6666cc; font-weight: bold;">int</span> servings<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      ServingSize <span style="color: #008000;">=</span> servingSize<span style="color: #008000;">;</span>
      Servings <span style="color: #008000;">=</span> servings<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder Calories<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> calories<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> Calories <span style="color: #008000;">=</span> calories<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NutritionFacts Build<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> NutritionFacts<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> NuitritionFacts<span style="color: #008000;">&#40;</span>Builder builder<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    ServingSize <span style="color: #008000;">=</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">ServingSize</span><span style="color: #008000;">;</span>
    Servings <span style="color: #008000;">=</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Servings</span><span style="color: #008000;">;</span>
    Calories <span style="color: #008000;">=</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Calories</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">...</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If you try to compile that code, the C# compiler will complain, saying that it does not have permission to access the private members of the Builder class.</p>
<p>Then how come <a href="http://stackoverflow.com/questions/1080383/why-doesnt-blochs-builder-pattern-work-in-c">it worked in Java</a>?  The answer is that Java has a special rule that allows Java outer classes to access the private members of their inner classes.  In C#, this rule does not exist, and thus C# outer classes cannot access the private members of C# inner classes.</p>
<p>Since Bloch&#8217;s Builder pattern doesn&#8217;t work in C#, the natural next question is: <a href="http://stackoverflow.com/questions/313729/how-to-implement-and-extend-joshuas-builder-pattern-in-net">how do we make it work</a>?</p>
<h3>Approach 1: Make Builder Properties Public</h3>
<p>The C# compiler tells us that it can&#8217;t compile because it doesn&#8217;t have permission to access private members of the nested Builder class.  Thus, the most obvious solution to this is to make them public:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span> 
  <span style="color: #008000;">...</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>      
    <span style="color: #008000;">...</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This allows the code to compile, and we can use the Builder just as we used it in Java.</p>
<p>However there is one niggling concern: the C# Builder exposes getters in the Builder object.  In the Java Builder, when we were constructing an object, we were only able to use methods that contributed to building the object.  That is, the Java Builder doesn&#8217;t expose any getters.  However, in the C# version, our API is polluted by unneeded getters.  Let&#8217;s see if we can do better.</p>
<h3>Approach 2: Build the object in the Builder</h3>
<p>As we&#8217;ve already seen, C# does not permit outer classes to access the private members of inner classes.  However, C# <em>does</em> let us access outer classes from inner classes.  Since that&#8217;s the case, why don&#8217;t we invert the building procedure.  That is, let&#8217;s build the object in the Builder, instead of in the class being built:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>    
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> servingSize, <span style="color: #6666cc; font-weight: bold;">int</span> servings<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      ServingSize <span style="color: #008000;">=</span> servingSize<span style="color: #008000;">;</span>
      Servings <span style="color: #008000;">=</span> servings<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder Calories<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> calories<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> Calories <span style="color: #008000;">=</span> calories<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>    
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NutritionFacts Build<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> NutritionFacts
      <span style="color: #008000;">&#123;</span>
        ServingSize <span style="color: #008000;">=</span> ServingSize,
        Servings <span style="color: #008000;">=</span> Servings,
        Calories <span style="color: #008000;">=</span> Calories,
        <span style="color: #008000;">...</span>       
      <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> NuitritionFacts<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span> 
    <span style="color: #008080; font-style: italic;">// Intentionally empty.</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>That, to me, is the closest you can get to the Java pattern.  From an API consumer&#8217;s point of view, it&#8217;ll look identical to a Java Builder.</p>
<h3>Alternative Form</h3>
<p>There is another alternative to the two C# Builders mentioned earlier in this article.  As it stands, Bloch&#8217;s Builder can be used as a sort of prototype factory.  You can configure the builder and then pump out instances of the class that have that configuration.  However, if you&#8217;re using the Builder to make a single, immutable instance, a more efficient way would be to avoid copying the data first to the Builder and then to the new instance.</p>
<p>Here&#8217;s a <a href="http://stackoverflow.com/questions/313729/how-to-implement-and-extend-joshuas-builder-pattern-in-net/313747#313747">version of the Builder</a> (by Jon Skeet) that can be used only once, but avoids unnecessary data copying.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NutritionFacts
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ServingSize <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Servings <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Calories <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">...</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Builder
  <span style="color: #008000;">&#123;</span>       
    <span style="color: #0600FF; font-weight: bold;">private</span> NutritionFacts _facts <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NutritionFacts<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> servingSize, <span style="color: #6666cc; font-weight: bold;">int</span> servings<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      _facts<span style="color: #008000;">.</span><span style="color: #0000FF;">ServingSize</span> <span style="color: #008000;">=</span> servingSize<span style="color: #008000;">;</span>
      _facts<span style="color: #008000;">.</span><span style="color: #0000FF;">Servings</span> <span style="color: #008000;">=</span> servings<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Builder Calories<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> calories<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span> _facts<span style="color: #008000;">.</span><span style="color: #0000FF;">Calories</span> <span style="color: #008000;">=</span> calories<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">...</span>    
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NutritionFacts Build<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      var ret <span style="color: #008000;">=</span> facts<span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">// Invalidates builder to maintain mutability.</span>
      facts <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">return</span> ret<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> NuitritionFacts<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span> 
    <span style="color: #008080; font-style: italic;">// Intentionally empty.</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Some of the advantages of this pattern are:</p>
<ul>
<li>No data copying is required at build time. In other words, while you&#8217;re setting the properties, you&#8217;re doing so on the real object &#8211; you just can&#8217;t see it yet. This is similar to what StringBuilder does.</li>
<li>The builder becomes invalid after calling Build() to guarantee immutability. This unfortunately means it can&#8217;t be used as a sort of &#8220;prototype&#8221; in the way that Bloch&#8217;s version can.</li>
</ul>
<h3>Conclusion</h3>
<p>In this article we&#8217;ve looked at 3 different working C# implementations of Bloch&#8217;s Builder pattern.  The first approach was a quick and dirty fix that got the job done.  The second approach applied a bit more finesse and managed to keep the same API as the Java Builder.  The final approach was more of a variation on the second approach, offering a more efficient implementation of the pattern if you&#8217;re not using the Builder as an object prototype factory.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/03/joshua-blochs-builder-pattern-in-csharp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

