<?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"
	>

<channel>
	<title>private override</title>
	<atom:link href="http://jonfuller.codingtomusic.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonfuller.codingtomusic.com</link>
	<description></description>
	<pubDate>Thu, 11 Dec 2008 15:49:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>static reflection - Method Guards</title>
		<link>http://jonfuller.codingtomusic.com/2008/12/11/static-reflection-method-guards/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/12/11/static-reflection-method-guards/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 15:48:02 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2008/12/11/static-reflection-method-guards/</guid>
		<description><![CDATA[Another bit of magic with static reflection. Refer to my last post for a little more explanation of static reflection, and a sweet implementation (IMO) of INotifyPropertyChanged using static reflection.
In the past several years, on several projects, I&#8217;ve seen many lines of code at the beginning of public interface methods that look like:
   [...]]]></description>
			<content:encoded><![CDATA[<p>Another bit of magic with static reflection. Refer to my <a href="http://jonfuller.codingtomusic.com/2008/12/08/static-reflection-inotifypropertychanged/">last post</a> for a little more explanation of static reflection, and a sweet implementation (IMO) of INotifyPropertyChanged using static reflection.</p>
<p>In the past several years, on several projects, I&#8217;ve seen many lines of code at the beginning of public interface methods that look like:</p>
<pre class="prettyprint">    public class WebServer
    &#123;
        public void BootstrapServer(int port, string rootDirectory, string serverName)
        &#123;
            if( rootDirectory == null )
            &#123;
                throw new ArgumentNullException( "rootDirectory" );
            &#125;
            if( serverName == null )
            &#123;
                throw new ArgumentNullException( "serverName" );
            &#125;

            // Bootstrap the server
        &#125;
    &#125;
</pre>
<p>I realize the need, since we don&#8217;t have real <a href="http://en.wikipedia.org/wiki/Design_by_contract">DbC</a> in .NET (yet), but I REALLY dislike this and feel it to be a pretty bad <a href="http://c2.com/xp/CodeSmell.html">smell</a>.&nbsp; It feels <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">unDRY</a>, obscures essence, and I just plain don&#8217;t like it. <img src='http://jonfuller.codingtomusic.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So, here is an implementation that makes it tolerable, if not enjoyable (because of the coolness of using static reflection), because I think it actually adds to the essence and overall readability of a given method.</p>
<p>This is what the above code can now look like:</p>
<pre class="prettyprint">    public class WebServer
    &#123;
        public void BootstrapServer( int port, string rootDirectory, string serverName )
        &#123;
            Guard.IsNotNull( () =&#62; rootDirectory );
            Guard.IsNotNull( () =&#62; serverName );

            // Bootstrap the server
        &#125;
    &#125;</pre>
<p>And here is the implementation:</p>
<pre class="prettyprint">    public static class Guard
    &#123;
        public static void IsNotNull&#60;T&#62;(Expression&#60;Func&#60;T&#62;&#62; expr)
        &#123;
            // expression value != default of T
            if (!expr.Compile()().Equals(default(T)))
                return;

            var param = (MemberExpression) expr.Body;
            throw new ArgumentNullException(param.Member.Name);
        &#125;
    &#125;</pre>
<p>Lovin it!&nbsp; Hope you do too.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/12/11/static-reflection-method-guards/feed/</wfw:commentRss>
		</item>
		<item>
		<title>static reflection - INotifyPropertyChanged</title>
		<link>http://jonfuller.codingtomusic.com/2008/12/08/static-reflection-inotifypropertychanged/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/12/08/static-reflection-inotifypropertychanged/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 15:45:22 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=56</guid>
		<description><![CDATA[I&#8217;m not going to delve into the definition of static reflection here, but you&#8217;ll see how it&#8217;s implemented, and how I found it can help me with implementing INotifyPropertyChanged.
Now, I&#8217;ve never been a fan of INotifyPropertyChanged myself, mostly because it feels wet (read: not DRY).  I&#8217;m working on a WPF app at the moment, and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not going to delve into the definition of <a href="http://www.clariusconsulting.net/blogs/kzu/archive/2007/12/30/49063.aspx">static</a> <a href="http://www.chadmyers.com/blog/archive/2007/12/31/static-reflection-using-expression-trees.aspx">reflection</a> <a href="http://ayende.com/Blog/archive/2005/10/29/StaticReflection.aspx">here</a>, but you&#8217;ll see how it&#8217;s implemented, and how I found it can help me with implementing <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx">INotifyPropertyChanged</a>.</p>
<p>Now, I&#8217;ve never been a fan of INotifyPropertyChanged myself, mostly because it feels wet (read: not DRY).  I&#8217;m working on a WPF app at the moment, and have come to realize, if I embrace INotifyPropertyChanged, I get to take full advantage of the excellent WPF databinding story.</p>
<p>Here is what a normal use of INotifyPropertyChanged looks like:</p>
<pre class="prettyprint">    public class Person : INotifyPropertyChanged
    {
        private string _firstName;
        private string _lastName;

        public event PropertyChangedEventHandler PropertyChanged;

        public string FirstName
        {
            get { return _firstName; }
            set
            {
                if (_firstName != value)
                {
                    _firstName = value;
                    FirePropertyChanged("FirstName");
                }
            }
        }

        public string LastName
        {
            get { return _lastName; }
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    FirePropertyChanged("LastName");
                }
            }
        }

        private void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }</pre>
<p>Here is what the static reflection use of INotifyPropertyChanged looks like:</p>
<pre class="prettyprint">    public class PersonStaticReflection : NotifyPropertyChanged
    {
        private string _firstName;
        private string _lastName;

        public string FirstName
        {
            get { return _firstName; }
            set { SetProperty(()=> FirstName, ()=> _firstName, value); }
        }

        public string LastName
        {
            get { return _lastName; }
            set
            {
                SetProperty( () => LastName, () => _lastName, value, () =>
                   {
                       // do something useful here
                   });
            }
        }
    }</pre>
<p>Doesn&#8217;t that just feel DRYer?  It does to me!</p>
<p>There are several important pieces to note:</p>
<ul>
<li>Inheriting from NotifyPropertyChanged, this is the class that holds the SetProperty method, and hides the static reflection magic.</li>
<li>We&#8217;re calling SetProperty with three arguments in the FirstName property
<ul>
<li>first: a LINQ Expression pointing to the property we&#8217;re changing</li>
<li>second: a LINQ Expression pointing to the field to backing the property</li>
<li>third: the new, incoming value</li>
</ul>
</li>
<li>We&#8217;re calling SetProperty with a fourth argument in the LastName property
<ul>
<li>fourth: an action that will get executed only if the values were different</li>
</ul>
</li>
<p>The SetProperty method, is going to look at the current value of the field, and the incoming value.  If the two are different, it will assign the new value to the field, and then fire the NotifyPropertyChanged event, with the name of the property given via the first argument.  I ended up pulling this into its own class so I could use it as the <a href="http://martinfowler.com/eaaCatalog/layerSupertype.html">layer supertype</a> in my <em>View Model </em>layer. Here is the implementation:</ul>
<pre class="prettyprint">    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void SetProperty < T>( Expression < Func<T>> propExpr, Expression<Func<T>> fieldExpr, T value )
        {
            SetProperty(propExpr, fieldExpr, value, ()=> { });
        }

        protected void SetProperty < T>( Expression < Func<T>> propExpr, Expression < Func < T>> fieldExpr, T value, Action doIfChanged )
        {
            var prop = (PropertyInfo)((MemberExpression)propExpr.Body).Member;
            var field = (FieldInfo)((MemberExpression)fieldExpr.Body).Member;

            var currVal = prop.GetValue( this, null );

            if( currVal == null &amp;&amp; value == null )
                return;
            if( currVal == null || !currVal.Equals( value ) )
            {
                field.SetValue( this, value );
                doIfChanged();

                if( PropertyChanged != null )
                    PropertyChanged( this, new PropertyChangedEventArgs( prop.Name ) );
            }
        }
    }</pre>
<p>Questions/comments/likes/dislikes? Let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/12/08/static-reflection-inotifypropertychanged/feed/</wfw:commentRss>
		</item>
		<item>
		<title>indy alt.net call for speakers!</title>
		<link>http://jonfuller.codingtomusic.com/2008/11/21/indy-altnet-call-for-speakers/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/11/21/indy-altnet-call-for-speakers/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 14:33:56 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2008/11/21/indy-altnet-call-for-speakers/</guid>
		<description><![CDATA[This doesn&#8217;t really have to be tightly coupled to indy alt.net, its really about general evangelization (did I just make that word up?&#8230; eh, sounds right), but this started out as an email from me to some folks here in our indy tech community to see if we can get some good quality content coming [...]]]></description>
			<content:encoded><![CDATA[<p>This doesn&#8217;t really have to be tightly coupled to <a href="http://www.indyalt.net">indy alt.net</a>, its really about general evangelization (did I just make that word up?&#8230; eh, sounds right), but this started out as an email from me to some folks here in our indy tech community to see if we can get some good quality content coming our way.&nbsp; Feel free to replace (in your brain) <a href="http://www.indyalt.net">Indy Alt.NET</a> with &#8220;Technology X&#8221;, or &#8220;User Group Y&#8221;</p>
<p>I know, as soon as you read the title for this, your brain started making up excuses why you wouldn&#8217;t ever be able to contribute to a group in this way.&nbsp; Believe me, been there, done that. <strong>Get. Over. It!</strong>&nbsp; Here is a list of your excuses, and reasons why they&#8217;re not valid.&nbsp; If you have any other excuses I missed, I&#8217;d be glad to tell you why its invalid as well <img src='http://jonfuller.codingtomusic.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<ul>
<li>Excuse #1: <strong>I don&#8217;t have a good story/content.</strong></li>
<ul>
<li><strong>FALSE</strong>: You almost certainly know something intimately that none of the rest of us know.&nbsp; We are all thirsting for information, do us a favor and share it with us!</li>
</ul>
<li>Excuse #2:&nbsp; <strong>I&#8217;m not an expert on anything.</strong></li>
<ul>
<li><strong>DOESN&#8217;T MATTER</strong>: What a coincidence!&nbsp; None of the rest of us are either!&nbsp; Just remember, &#8220;there are no smart guys, just us&#8221;&#8230; that includes you!&nbsp; If you REALLY don&#8217;t know anything cool enough to talk about&#8230; go spend 10-20 hours learning something new/cool, distilling it down to 60 minutes so we can all leech off your brain.</li>
</ul>
<li>Excuse #3:&nbsp; <strong>I&#8217;m scared/anxious/have issues talking in front of people/etc.</strong></li>
<ul>
<li><strong>GREAT!</strong>: What better way to practice than in a small intimate setting with a few of like minded peers?&nbsp; This is a great opportunity to learn new things, and step out of your comfort zone to grow personally and professionally.</li>
</ul>
<li>Excuse #4:&nbsp; <strong>I suck at PowerPoint.</strong></li>
<ul>
<li><strong>ALL THE BETTER</strong>:&nbsp; I, personally, would rather see code than PowerPoint any day.</li>
</ul>
<li>Excuse #5:&nbsp; <strong>I don&#8217;t know .NET very well.</strong></li>
<ul>
<li><strong>AWESOME</strong>:&nbsp; We don&#8217;t necessarily care about .NET (our speaker last night is a non-.NET dude!).&nbsp; Our main goal is <strong>continuous improvement</strong>, and the <strong>practices and principles </strong>that will make us <strong>better</strong>.</li>
</ul>
</ul>
<p>I&#8217;d like to invoke a few loosely translated Fight Club Rules on us.<br />&nbsp; [Originals]<br />&nbsp; Rule #1:&nbsp; You don&#8217;t talk about fight club.<br />&nbsp; Rule #2:&nbsp; You don&#8217;t talk about fight club.<br />&nbsp; &#8230;<br />&nbsp; Rule #8&nbsp; If this is your first night at Fight Club, you have to fight.<br />&nbsp; [Translated]<br />&nbsp; Rule #1:&nbsp; You must talk about Indy Alt.NET<br />&nbsp; Rule #2:&nbsp; You must talk about Indy Alt.NET<br />&nbsp; &#8230;<br />&nbsp; Rule #8:&nbsp; If you have not presented before at Indy Alt.NET, then you have to present (sometime, not necessarily this upcoming time, I won&#8217;t hold you to it like Mr. Durden would).</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/11/21/indy-altnet-call-for-speakers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>indy alt.net - team roles fishbowl</title>
		<link>http://jonfuller.codingtomusic.com/2008/10/16/indy-altnet-team-roles-fishbowl/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/10/16/indy-altnet-team-roles-fishbowl/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 13:07:54 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2008/10/16/indy-altnet-team-roles-fishbowl/</guid>
		<description><![CDATA[We&#8217;re having another Indy Alt.Net (www.indyalt.net) meeting tonight!
The topic is on team roles. We&#8217;re planning on having a

BA - Sorry A-Team fans, this is a Business Analyst, not Mr. T
Developers - All who serve at different levels in their own organizations
PM - [Hopefully] If you&#8217;re a PM, and can come, please do, we need you!

The [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re having another Indy Alt.Net (<a href="http://www.indyalt.net">www.indyalt.net</a>) meeting tonight!</p>
<p>The topic is on team roles. We&#8217;re planning on having a</p>
<ul>
<li>BA - Sorry <a href="http://en.wikipedia.org/wiki/The_A-Team">A-Team</a> fans, this is a Business Analyst, not <a href="http://en.wikipedia.org/wiki/Mr._T">Mr. T</a></li>
<li>Developers - All who serve at different levels in their own organizations</li>
<li>PM - [Hopefully] If you&#8217;re a PM, and can come, please do, we need you!</li>
</ul>
<p>The goal is really just to have good conversation about different roles in different organizations so we can all learn something by the way other organizations behave.</p>
<p>As usual, free pizza and networking starting at 5:15, with the talk starting at 6:00.</p>
<p>Logistics:<br />Tonight (October 16)<br />6:00P - 8:00P (food and networking at 5:15)<br />Ivy Tech near Fall Creek and Meridian on the north side of Fall Creek<br />Auditorium, 4th Floor 50 W Fall Creek Pkwy North Dr<br />Indianapolis, IN 46208<br /><a href="http://maps.google.com/maps?f=q&amp;hl=en&amp;geocode=&amp;q=Ivy+Tech+State+College,+50+W+Fall+Creek+Pkwy+N+Dr&amp;jsv=115&amp;sll=39.804547,-86.157596&amp;sspn=0.006215,0.0078&amp;ie=UTF8&amp;latlng=39803466,-86157732,9025412448198445800&amp;ei=Fd9PSNGwG6TIigHN98C9Cw&amp;cd=1">Map</a></p>
<p>Hope to see you there!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/10/16/indy-altnet-team-roles-fishbowl/feed/</wfw:commentRss>
		</item>
		<item>
		<title>reflection</title>
		<link>http://jonfuller.codingtomusic.com/2008/10/03/reflection/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/10/03/reflection/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 15:22:30 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[goals]]></category>

		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=51</guid>
		<description><![CDATA[It being a day or several (depending on when this gets published) after my birthday, it seems like a good time to reflect, and to set some goals for the upcoming short-term (~year).
The list is not all about technical stuff.  It is really stemmed from some things God has laid in front of me lately.
Becoming [...]]]></description>
			<content:encoded><![CDATA[<p>It being a day or several (depending on when this gets published) after my birthday, it seems like a good time to reflect, and to set some goals for the upcoming short-term (~year).</p>
<p>The list is not all about technical stuff.  It is really stemmed from some things God has laid in front of me lately.</p>
<h4>Becoming a message</h4>
<p>Our pastor shared a great quote with our <a href="http://www.commonwaychurch.com/">church</a> last weekend that really stood out to me.</p>
<blockquote><p>&#8230;my life is my message  - Gandhi</p></blockquote>
<p>What would my message be if someone asked me today?<br />
What would my message be if someone asked <em>you </em>today?<br />
What would <em>your</em> message be if someone asked <em>you </em>today?</p>
<p>I&#8217;m guessing there will be more about this coming soon.</p>
<h4>Continuous Improvement</h4>
<p>This area is far more about <em>you</em> than it is about me.  Sure, I have much to learn, have a long list of areas of which I&#8217;d like to personally continue to improve.I feel a great need to help my colleagues, help my company, and help my community.  Something tells me I&#8217;m in a unique position to make an impact on each.So here is a list of areas I&#8217;d like to improve (in to particular order):</p>
<ul>
<li>Technical Breadth</li>
<li>Technical Depth</li>
<li>Leadership Skills</li>
<li>Management Skills</li>
<li>Physical Health</li>
<li>Faith</li>
<li>Creativity</li>
<li>Being a better husband</li>
<li>Brewing better coffee</li>
<li>&#8230;</li>
</ul>
<h4>Back to Basics</h4>
<p>I&#8217;m a firm believer in fundamentals.  I&#8217;d like to re-explore what the core fundamentals of the industry are, and find out where I need to brush up/start over/start (for the first time).</p>
<p>Okay&#8230; enough for now.  Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/10/03/reflection/feed/</wfw:commentRss>
		</item>
		<item>
		<title>IndyTechFest PocketMod</title>
		<link>http://jonfuller.codingtomusic.com/2008/10/01/indytechfest-pocketmod/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/10/01/indytechfest-pocketmod/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 12:06:53 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=47</guid>
		<description><![CDATA[It&#8217;s October in Indy (well, most other places too), so that means time for IndyTechFest.
For those of you that read this, or find this, I&#8217;ve put together a PocketMod for the session schedule.  IMO, the UI is easier to use than the mini poster.  I&#8217;m sharing so you can have one too!
Here it is:  IndyTechFest [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s October in Indy (well, most other places too), so that means time for <a href="http://www.indytechfest.com">IndyTechFest</a>.</p>
<p>For those of you that read this, or find this, I&#8217;ve put together a <a href="http://www.pocketmod.com">PocketMod</a> for the session schedule.  IMO, the UI is easier to use than the <a href="http://www.indytechfest.com/portals/10/IndyTechFest_2008_MiniPoster.pdf">mini poster</a>.  I&#8217;m sharing so you can have one too!</p>
<p>Here it is:  <a title="IndyTechFest PocketMod" href="http://jonfuller.codingtomusic.com/wp-content/uploads/2008/10/pocketmodpm.pdf" target="_blank">IndyTechFest PocketMod</a></p>
<p>Here is how I did it:</p>
<ul>
<li>Used Word to create this: <a title="Word 2007 PocketMod source" href="http://jonfuller.codingtomusic.com/wp-content/uploads/2008/10/pocketmod.docx" target="_blank">PocketMod source</a></li>
<li>Used the Office 2007 PDF Publisher to publish it to a PDF</li>
<li>Used the <a href="http://www.pocketmod.com/files/PDFtoPocketmod.exe">PDFtoPocketmod</a> converter app to convert the 8-page PDF to a 1-page PDF with all the pages squished and arranged just right</li>
<li>Printed, and trimmed the margins with a paper cutter</li>
<li>Folded and snipped, via the video at <a href="http://www.pocketmod.com">Pocketmod</a>.</li>
</ul>
<p>Super simple, super awesome.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/10/01/indytechfest-pocketmod/feed/</wfw:commentRss>
		</item>
		<item>
		<title>a generic one-to-one mapping class</title>
		<link>http://jonfuller.codingtomusic.com/2008/09/17/a-generic-one-to-one-mapping-class/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/09/17/a-generic-one-to-one-mapping-class/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 16:13:27 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Tech]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=38</guid>
		<description><![CDATA[Another &#8220;tip/trick&#8221; style post, but thought it may prove useful to someone (or so I can find it again later!)&#8230;
Sometimes I find myself needing to map instances/values of one type to another.&#160; For example, on my current project we have an enumeration in our communications layer, and a VERY similar one in the domain layer.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Another &#8220;tip/trick&#8221; style post, but thought it may prove useful to someone (or so I can find it again later!)&#8230;</p>
<p>Sometimes I find myself needing to map instances/values of one type to another.&nbsp; For example, on my current project we have an enumeration in our communications layer, and a VERY similar one in the domain layer.&nbsp; The reason for this is so we don&#8217;t have to be referencing the communications layer version all over in the domain code.&nbsp; So, there is naturally a one to one mapping between these enumerations.</p>
<p>Consider the following enumerations:</p>
<p>
<pre class="prettyprint">enum Polygons
{
    Triangle,
    Rectangle,
    Pentagon,
    Other
}

enum NumSides
{
    Three,
    Four,
    Five,
    More
}
</pre>
</p>
<p>This is the code I usually write to solve this problem:</p>
<p>
<pre class="prettyprint">private Polygons MapShapes( NumSides sides )
{
    switch( sides )
    {
        case NumSides.Three:
            return Polygons.Triangle;
        case NumSides.Four:
            return Polygons.Rectangle;
        case NumSides.Five:
            return Polygons.Pentagon;
        case NumSides.More:
            return Polygons.Other;
        default:
            throw new NotSupportedException(
                string.Format( "Mapping to Polygons from NumSides value {0}
                    is not supported", sides.ToString() ) );
    }
}

private NumSides MapShapes( Polygons polygon )
{
    switch( polygon )
    {
        case Polygons.Triangle:
            return NumSides.Three;
        case Polygons.Rectangle:
            return NumSides.Four;
        case Polygons.Pentagon:
            return NumSides.Five;
        case Polygons.Other:
            return NumSides.More;
        default:
            throw new NotSupportedException(
                string.Format( "Mapping to NumSides from Polygons value {0}
                    is not supported", polygon.ToString() ) );
    }
}
</pre>
</p>
<p>It turns out we had 3 said enumerations.&nbsp; So picture the above, copied 3 times.&nbsp; This thing was just begging for abstraction!</p>
<p>I came up with a Generic (proper) Map class that you can add mappings to and retrieve the corresponding mapped value of the other type&#8230; both directions.&nbsp; Here is the implementation:</p>
<p>
<pre class="prettyprint">public class Map<Type1, Type2>
{
    private Dictionary<Type1, Type2> _oneToTwo;
    private Dictionary<Type2, Type1> _twoToOne;

    public Map()
    {
        _oneToTwo = new Dictionary<Type1, Type2>();
        _twoToOne = new Dictionary<Type2, Type1>();
    }

    public Map<Type1, Type2> Add( Type1 value1, Type2 value2 )
    {
        _oneToTwo.Add( value1, value2 );
        _twoToOne.Add( value2, value1 );
        return this;
    }

    public Type1 this[Type2 value]
    {
        get
        {
            if( !_twoToOne.ContainsKey( value ) )
            {
                throw new NotSupportedException(
                    string.Format( "Mapping from type {0} to type {1} with
                                    value {2} is not defined",
                        typeof( Type2 ).Name,
                        typeof( Type1 ).Name,
                        value.ToString() ) );
            }
            return _twoToOne[value];
        }
    }

    public Type2 this[Type1 value]
    {
        get
        {
            if( !_oneToTwo.ContainsKey( value ) )
            {
                throw new NotSupportedException(
                    string.Format( "Mapping from type {0} to type {1} with
                                    value {2} is not defined",
                        typeof( Type1 ).Name,
                        typeof( Type2 ).Name,
                        value.ToString() ) );
            }
            return _oneToTwo[value];
        }
    }
}
</pre>
</p>
<p>And, here it is in action:</p>
<p>
<pre class="prettyprint">
Map&#60;NumSides, Polygons&#62; shapeMap = new Map&#60;NumSides, Polygons&#62;()
                                    .Add( NumSides.Three, Polygons.Triangle )
                                    .Add( NumSides.Four, Polygons.Rectangle )
                                    .Add( NumSides.Five, Polygons.Pentagon )
                                    .Add( NumSides.More, Polygons.Other );

// shape1 value is Polygons.Rectangle
Polygons shape1 = shapeMap[NumSides.Four]; 

// shape2 value is NumSides.Three
NumSides shape2 = shapeMap[Polygons.Triangle];
</pre>
<p> This not only works with enumerations, but any type.&nbsp; I see an extension to this possibly allowing for a default mapping, and possibly constraining Type1 and Type2 to be <code>IComparable </code>or something like that as well, so you can have more control over your matching than whatever the default matching behavior that the Dictionary uses is.</p>
<p>That&#8217;s it&#8230; enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/09/17/a-generic-one-to-one-mapping-class/feed/</wfw:commentRss>
		</item>
		<item>
		<title>looking for a better switch/case statement?</title>
		<link>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 15:08:01 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Tech]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/?p=25</guid>
		<description><![CDATA[I find myself sometimes wishing I had a better switch/case construct in C#.
My requirements of &#8220;better&#8221; are:

Case statements are automatically scoped
I can switch on more than simple types
I can have custom logic for my matching criteria

But why not just use if/else if/else&#8230;?
Good question, I&#8217;m glad you asked!  However, I don&#8217;t have an excellent answer [...]]]></description>
			<content:encoded><![CDATA[<p>I find myself sometimes wishing I had a better switch/case construct in C#.</p>
<p>My requirements of &#8220;better&#8221; are:</p>
<ul>
<li>Case statements are automatically scoped</li>
<li>I can switch on more than simple types</li>
<li>I can have custom logic for my matching criteria</li>
</ul>
<h3>But why not just use if/else if/else&#8230;?</h3>
<p>Good question, I&#8217;m glad you asked!  However, I don&#8217;t have an excellent answer other than personal preference.  if/else if/else is okay sometimes, but other times I have a deterministic set of conditions, and a switch type construct feels more symmetrical to me.  I loves me some symmetric feeling code, it just feels cleaner somehow.</p>
<p>So I&#8217;ll present a somewhat contrived example just so you can see what the usage looks like, then we&#8217;ll see the code.</p>
<h3>Usage</h3>
<pre class="prettyprint">

public void DoSomething()
&#123;
	object foo = new object();
	object bar = new object();
	object baz = new object();

	Switch&#60;object&#62;.On(baz)
		.Case(x =&#62; x.Equals(foo), () =&#62;
		&#123;
			Console.WriteLine("came into foo case");
		&#125;)
		.Case(x =&#62; x.Equals(bar), () =&#62;
		&#123;
			Console.WriteLine("came into bar case");
		&#125;)
		.Default( () =&#62;
		&#123;
			Console.WriteLine("came into default case");
		&#125;)
		.Go();
&#125;
</pre>
<p>The output from the above, is as you&#8217;d expect, the default case, since baz wasn&#8217;t equal to foo or bar.</p>
<h3>Implementation</h3>
<pre class="prettyprint">

public class Switch&#60;T&#62;
&#123;
	private T _target;
	private List&#60;KeyValuePair&#60;Predicate&#60;T&#62;, Action&#62;&#62; _cases;
	private Action _default;

	public static Switch&#60;T&#62; On(T target)
	&#123;
		return new Switch&#60;T&#62;(target);
	&#125;

	public Switch(T target)
	&#123;
		_target = target;
		_cases = new List&#60;KeyValuePair&#60;Predicate&#60;T&#62;, Action&#62;&#62;();
	&#125;

	public Switch&#60;T&#62; Case(Predicate&#60;T&#62; @case, Action action)
	&#123;
		_cases.Add(new KeyValuePair&#60;Predicate&#60;T&#62;, Action&#62;(@case, action));
		return this;
	&#125;

	public Switch&#60;T&#62; Default(Action action)
	&#123;
		_default = action;
		return this;
	&#125;

	public void Go()
	&#123;
		foreach(var @case in _cases)
			if (@case.Key(_target))
			&#123;
				@case.Value();
				return;
			&#125;
		if (_default != null)
			_default();
	&#125;
&#125;
</pre>
<p>The only part I don&#8217;t really like is having to kick it off with the Go() at the end. Any ideas for a better way to kick it off, or at least a better name for it (instead of Go).</p>
<p>Feedback? Ideas?</p>
<h3>Update</h3>
<p>Paul had <del datetime="2008-09-05T12:09:08+00:00">a great idea</del> a couple great ideas (see comments) on how to get rid of the Go() at the end, and to force Default to only appear at the end.  Here is the new implementation.  Usage is the same, just remove the Go().</p>
<pre class="prettyprint">

public class Switch&#60;T&#62;
&#123;
	private T _target;
	private bool _alreadyMatched;

	public static Switch&#60;T&#62; On(T target)
	&#123;
		return new Switch&#60;T&#62;(target);
	&#125;

	public Switch(T target)
	&#123;
		_target = target;
		_alreadyMatched = false;
	&#125;

	public Switch&#60;T&#62; Case(Predicate&#60;T&#62; @case, Action action)
	&#123;
		if (!_alreadyMatched &#038;&#038; @case(_target))
		&#123;
			_alreadyMatched = true;
			action();
		&#125;
		return this;
	&#125;

	public void Default(Action action)
	&#123;
		if (!_alreadyMatched)
		&#123;
			_alreadyMatched = true;
			action();
		&#125;
		return this;
	&#125;
&#125;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/09/04/looking-for-a-better-switchcase-statement/feed/</wfw:commentRss>
		</item>
		<item>
		<title>chrome&#8230; c&#8217;mon</title>
		<link>http://jonfuller.codingtomusic.com/2008/09/03/chrome-cmon/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/09/03/chrome-cmon/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 15:06:57 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2008/09/03/chrome-cmon/</guid>
		<description><![CDATA[So I did what every other self-respecting geek has done in the past couple days&#8230; downloaded and installed chrome.&#160; I love my firefox experience at the moment (I have the exact add-ons/plugins I want/need to make browsing pleasurable and fast), but I thought I&#8217;d give it a shot.
However, I was greeted with this (and I [...]]]></description>
			<content:encoded><![CDATA[<p>So I did what every other self-respecting geek has done in the past couple days&#8230; downloaded and installed chrome.&#160; I love my firefox experience at the moment (I have the exact add-ons/plugins I want/need to make browsing pleasurable and fast), but I thought I&#8217;d give it a shot.</p>
<p>However, I was greeted with this (and I can&#8217;t seem to get anything other than this at the moment).&#160; Yes, I tried pressing reload or going to another page&#8230; didn&#8217;t work.</p>
<p><a href="http://jonfuller.codingtomusic.com/wp-content/uploads/2008/09/windowslivewriterchromecmon-93daimage-2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://jonfuller.codingtomusic.com/wp-content/uploads/2008/09/windowslivewriterchromecmon-93daimage-thumb.png" width="208" border="0" /></a> </p>
<p>That flat-out stinks (not because I personally can&#8217;t use it, but because I&#8217;ve got no idea what to do to fix it, or what went wrong).&#160; Do I have to restart first?&#160; Do I have to install some WebKit thing first?&#160; Do I have to &lt;insert other thing I shouldn&#8217;t have to do here&gt; first?</p>
<p>Maybe I&#8217;m just clueless, but this was a bad first impression.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/09/03/chrome-cmon/feed/</wfw:commentRss>
		</item>
		<item>
		<title>tfs - using winmerge as your merge/diff tool</title>
		<link>http://jonfuller.codingtomusic.com/2008/08/26/tfs-using-winmerge-as-your-mergediff-tool/</link>
		<comments>http://jonfuller.codingtomusic.com/2008/08/26/tfs-using-winmerge-as-your-mergediff-tool/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 15:07:43 +0000</pubDate>
		<dc:creator>jon</dc:creator>
		
		<category><![CDATA[Tech]]></category>

		<category><![CDATA[tfs]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2008/08/26/tfs-using-winmerge-as-your-mergediff-tool/</guid>
		<description><![CDATA[If you&#8217;re used to using something like winmerge with your SCM of choice, if you wind up using TFS, the built-in diff/merge tools can leave much to be desired.
Here is how to change the tool VS 2kX uses to do diffs/merges when hooked up to TFS (note that these are my settings for using winmerge [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re used to using something like <a href="http://winmerge.org/">winmerge </a>with your SCM of choice, if you wind up using TFS, the built-in diff/merge tools can leave much to be desired.</p>
<p>Here is how to change the tool VS 2kX uses to do diffs/merges when hooked up to TFS (note that these are <strong>my </strong>settings for using winmerge for <strong>ALL </strong>file types):</p>
<p>Go To: Tools/Options/SourceControl/Visual Studio Team Foundation Server/Configure User Tools</p>
<p>Click &#8220;Add&#8230;&#8221;<br />
Extension:  .*<br />
Operation: Compare<br />
Command: C:\Program Files\WinMerge\WinMergeU.exe<br />
Arguments: /e /wl /dl %6 /dr %7 %1 %2<br />
Click &#8220;Add&#8230;&#8221;</p>
<p>Extension:  .*<br />
Operation: Merge<br />
Command: C:\Program Files\WinMerge\WinMergeU.exe<br />
Arguments: /ub /dl %6 /dr %7 %1 %2 %4</p>
<p>The winmerge command line reference can be found <a href="http://winmerge.org/2.6/manual/CommandLine.html">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2008/08/26/tfs-using-winmerge-as-your-mergediff-tool/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
