<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>private override</title>
	
	<link>http://jonfuller.codingtomusic.com</link>
	<description />
	<lastBuildDate>Fri, 27 Aug 2010 16:43:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/jonfuller" /><feedburner:info uri="jonfuller" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/jonfuller" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare href="http://www.plusmo.com/add?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare href="http://www.thefreedictionary.com/_/hp/AddRSS.aspx?http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://img.tfd.com/hp/addToTheFreeDictionary.gif">Subscribe with The Free Dictionary</feedburner:feedFlare><feedburner:feedFlare href="http://www.bitty.com/manual/?contenttype=rssfeed&amp;contentvalue=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.bitty.com/img/bittychicklet_91x17.gif">Subscribe with Bitty Browser</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsalloy.com/?rss=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.newsalloy.com/subrss3.gif">Subscribe with NewsAlloy</feedburner:feedFlare><feedburner:feedFlare href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare href="http://mix.excite.eu/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://image.excite.co.uk/mix/addtomix.gif">Subscribe with Excite MIX</feedburner:feedFlare><feedburner:feedFlare href="http://www.yourminis.com/subscribe.aspx?u=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.yourminis.com/images/addtoyourminisbadge.gif">Subscribe with Yourminis.com</feedburner:feedFlare><feedburner:feedFlare href="http://download.attensa.com/app/get_attensa.html?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.attensa.com/blogs/attensa/WindowsLiveWriter/BadgeredintoBadges_10C02/attensa_feed_button5.gif">Subscribe with Attensa for Outlook</feedburner:feedFlare><feedburner:feedFlare href="http://www.webwag.com/wwgthis.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.webwag.com/images/wwgthis.gif">Subscribe with Webwag</feedburner:feedFlare><feedburner:feedFlare href="http://hub.netomat.net/account/account.autoSubscribe.jspa?urls=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.netomat.net/blogger/images/icon_netomat_feedbutton.gif">Subscribe with netomat Hub</feedburner:feedFlare><feedburner:feedFlare href="http://www.podcastready.com/oneclick_bookmark.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.podcastready.com/images/podcastready_button.gif">Subscribe with Podcast Ready</feedburner:feedFlare><feedburner:feedFlare href="http://www.flurry.com/pushRssFeed.do?r=fb&amp;url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.flurry.com/images/flurry_rss_logo2.gif">Subscribe with Flurry</feedburner:feedFlare><feedburner:feedFlare href="http://www.wikio.com/subscribe?url=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.wikio.com/shared/img/add2wikio.gif">Subscribe with Wikio</feedburner:feedFlare><feedburner:feedFlare href="http://www.dailyrotation.com/index.php?feed=http%3A%2F%2Ffeeds.feedburner.com%2Fjonfuller" src="http://www.dailyrotation.com/rss-dr2.gif">Subscribe with Daily Rotation</feedburner:feedFlare><item>
		<title>Never implement INotifyPropertyChanged again</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/fNeWCwaX89s/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/08/27/never-implement-inotifypropertychanged-again/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 16:25:35 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[dynamicproxy]]></category>
		<category><![CDATA[inotifypropertychanged]]></category>
		<category><![CDATA[structuremap]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/08/27/never-implement-inotifypropertychanged-again/</guid>
		<description><![CDATA[I hate every time I am working on something and I have to implement INotifyPropertyChanged.&#160; My DRY-dey sense tingles.&#160; Not only am I forced to not use auto-properties (1st DRY violation), I’m forced to fire the event in each setter (2nd DRY violation), and specify the name of the property that is getting set, inside [...]]]></description>
			<content:encoded><![CDATA[<p>I hate every time I am working on something and I have to implement <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx">INotifyPropertyChanged</a>.&#160; My <em><a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">DRY</a>-dey sense</em> tingles<em>.</em>&#160; Not only am I forced to not use auto-properties (1st DRY violation), I’m forced to fire the event in each setter (2nd DRY violation), and specify the name of the property that is getting set, inside of that property’s setter (3rd DRY violation).&#160; That much WET (read: not-DRY), for something so simple leaves me a little grumpy.</p>
<p>I’ve been on this <a href="http://jonfuller.codingtomusic.com/2008/12/08/static-reflection-inotifypropertychanged/">quest before</a>, to simplify this a bit, but it was still a little hackety, and limiting.</p>
<p>This time, I set out to do it right.</p>
<p>I’ll spare you most of the technical details, but it’s backed by <a href="http://castleproject.org/dynamicproxy/index.html">Castle’s DynamicProxy</a> project, and there’s some integration with <a href="http://structuremap.github.com/structuremap/index.html">StructureMap</a> to make it super easy, though you don’t really have to use <a href="http://structuremap.github.com/structuremap/index.html">StructureMap</a> if you don’t want to.&#160; [note:&#160; I’ll probably add more container support as I find time.&#160; If you have a specific need, let me know, or submit a patch.]</p>
<p>Here are the codez to show it in action:</p>
<h4></h4>
<h4>Basics</h4>
<h5>Using it for a class with an interface</h5>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #80ff00">// </span><span style="color: #00008b; font-weight: bold">note the attribute goes on the interface, not the class</span></p>
<p style="margin: 0px">[<span style="color: #64b1ff">AutoNotify</span>]</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">interface</span> <span style="color: #ff6fb7">IFoo</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">string</span> Value { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">Foo</span> : <span style="color: #ff6fb7">IFoo</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">string</span> Value { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<h5>Using it for a class</h5>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px">[<span style="color: #64b1ff">AutoNotify</span>(Fire <span style="color: silver">=</span> <span style="color: #d6adad">FireOptions</span><span style="color: silver">.</span>OnlyOnChange)]</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">Foo</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #80ff00">// </span><span style="color: #00008b; font-weight: bold">note for autonotify to work, the property must be virtual</span></p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span> Value { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>The previous example shows how to get the event to fire only when the value is different also.&#160; It defaults to always firing, whether the value changes or not.&#160; It’s also important to note that your properties need to be virtual so the calls to the setter can be intercepted.</p>
<h4>Dependent Properties</h4>
<p>Sometimes (usually) you’ve got calculated properties that need to fire the notified event too, these usually turn into WET mess as well.&#160; We’ve got the problem solved, and you’ve got a few different options, hopefully one of them suits you.</p>
<h5>Dependency Map – DependsOn</h5>
<p>You specify the type that defines the DependencyMap on the attribute, and then set up your dependencies in that type’s constructor.&#160; This style is somewhat influenced by the <a href="http://fluentnhibernate.org/">FluentNHibernate</a> API.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px">[<span style="color: #64b1ff">AutoNotify</span>(DependencyMap <span style="color: silver">=</span> <span style="color: #8ac5ff">typeof</span>(<span style="color: #64b1ff">ProjectDependency</span>))]</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">Project</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span> Name { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span>[] Files { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">int</span> FileCount { <span style="color: #8ac5ff">get</span> { <span style="color: #8ac5ff">return</span> Files<span style="color: silver">.</span>Length; } }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">ProjectDependency</span> : <span style="color: #64b1ff">DependencyMap</span><span style="color: silver">&lt;</span><span style="color: #64b1ff">Project</span><span style="color: silver">&gt;</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> ProjectDependency()</p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Property(x <span style="color: silver">=&gt;</span> x<span style="color: silver">.</span>FileCount)<span style="color: silver">.</span>DependsOn(x <span style="color: silver">=&gt;</span> x<span style="color: silver">.</span>Files);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<h5>Dependency Map – Updates</h5>
<p>If you’d rather express your dependency the other way around, that’s fine too.&#160; The two are equivalent.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px">[<span style="color: #64b1ff">AutoNotify</span>(DependencyMap <span style="color: silver">=</span> <span style="color: #8ac5ff">typeof</span>(<span style="color: #64b1ff">ProjectDependency</span>))]</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">Project</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span> Name { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span>[] Files { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">int</span> FileCount { <span style="color: #8ac5ff">get</span> { <span style="color: #8ac5ff">return</span> Files<span style="color: silver">.</span>Length; } }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">ProjectDependency</span> : <span style="color: #64b1ff">DependencyMap</span><span style="color: silver">&lt;</span><span style="color: #64b1ff">Project</span><span style="color: silver">&gt;</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> ProjectDependency()</p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Property(x <span style="color: silver">=&gt;</span> x<span style="color: silver">.</span>Files)<span style="color: silver">.</span>Updates(x <span style="color: silver">=&gt;</span> x<span style="color: silver">.</span>FileCount);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<h5>Dependency Map – UpdatesWith</h5>
<p>If you want to stick with an auto-property, and leave the calculated property logic somewhere else, you can hook it in via your dependency map too.&#160; This example, again, is equivalent to the previous two.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px">[<span style="color: #64b1ff">AutoNotify</span>(DependencyMap <span style="color: silver">=</span> <span style="color: #8ac5ff">typeof</span>(<span style="color: #64b1ff">ProjectDependency</span>))]</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">Project</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span> Name { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span>[] Files { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">int</span> FileCount { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">ProjectDependency</span> : <span style="color: #64b1ff">DependencyMap</span><span style="color: silver">&lt;</span><span style="color: #64b1ff">Project</span><span style="color: silver">&gt;</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> ProjectDependency()</p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Property(x <span style="color: silver">=&gt;</span> x<span style="color: silver">.</span>Files)<span style="color: silver">.</span>Updates(x <span style="color: silver">=&gt;</span> x<span style="color: silver">.</span>FileCount)<span style="color: silver">.</span>With(p <span style="color: silver">=&gt;</span> p<span style="color: silver">.</span>Files<span style="color: silver">.</span>Length);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<h5>DependsOn Attribute</h5>
<p>If you don’t like any of those options and are looking for something a little more simple, maybe you’ll like this one.&#160; Just specify which things your property depends on in an attribute.&#160; You lose your 100% static typing help, but it’s more concise.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px">[<span style="color: #64b1ff">AutoNotify</span>]</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">Project</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span> Name { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">string</span>[] Files { <span style="color: #8ac5ff">get</span>; <span style="color: #8ac5ff">set</span>; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; [<span style="color: #64b1ff">DependsOn</span>(<span style="color: #ff8040">&quot;Files&quot;</span>)]</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">virtual</span> <span style="color: #8ac5ff">int</span> FileCount { <span style="color: #8ac5ff">get</span> { <span style="color: #8ac5ff">return</span> Files<span style="color: silver">.</span>Length; } }</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<h4></h4>
<h4>Containers and otherwise</h4>
<h5>Hooking it into StructureMap</h5>
<p>There are a couple conventions you can use to hook into <a href="http://structuremap.github.com/structuremap/index.html">StructureMap</a>.&#160; There is the attribute convention (which is what you’re seeing above), and there is a generic predicate convention that you can use any predicate logic.&#160; Below you can see the attribute one getting hooked in.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> container <span style="color: silver">=</span> <span style="color: #8ac5ff">new</span> <span style="color: #64b1ff">Container</span>(config <span style="color: silver">=&gt;</span> config<span style="color: silver">.</span>Scan(scanConfig <span style="color: silver">=&gt;</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; scanConfig<span style="color: silver">.</span>With(<span style="color: #8ac5ff">new</span> <span style="color: #64b1ff">AutoNotifyAttrConvention</span>());</p>
<p style="margin: 0px">&#160;&#160;&#160; scanConfig<span style="color: silver">.</span>TheCallingAssembly();</p>
<p style="margin: 0px">&#160;&#160;&#160; scanConfig<span style="color: silver">.</span>WithDefaultConventions();</p>
<p style="margin: 0px">}));</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> project <span style="color: silver">=</span> container<span style="color: silver">.</span>GetInstance<span style="color: silver">&lt;</span><span style="color: #64b1ff">Project</span><span style="color: silver">&gt;</span>();</p>
</p></div>
<h5>Using it without StructureMap</h5>
<p>If you’re using another container, or no container at all, but want to use some other factory or something, you can do that too.&#160; This example is for something with an interface.&#160; It’s very similar to do the same for a concrete class… you just don’t instantiate the object first.&#160; You also have an opportunity to hook into the dependent property structure here as well with the DependencyMap parameter.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> foo <span style="color: silver">=</span> <span style="color: #8ac5ff">new</span> <span style="color: #64b1ff">Foo</span>();</p>
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> notifiableFoo <span style="color: silver">=</span> <span style="color: #64b1ff">Notifiable</span><span style="color: silver">.</span>MakeForInterface(</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">typeof</span>(<span style="color: #ff6fb7">IFoo</span>),</p>
<p style="margin: 0px">&#160;&#160;&#160; foo,</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #d6adad">FireOptions</span><span style="color: silver">.</span>Always,</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">new</span> <span style="color: #64b1ff">ProxyGenerator</span>(),</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #64b1ff">DependencyMap</span><span style="color: silver">.</span>Empty);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #64b1ff">Assert</span><span style="color: silver">.</span>That(notifiableFoo <span style="color: #8ac5ff">is</span> <span style="color: #ff6fb7">INotifyPropertyChanged</span>);</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<h4>Whew, done</h4>
<p>So… that’s a lot of ‘how to’, but hopefully it’ll be somewhat complete introduction to get you working with it.&#160; I really don’t see much of any reason to ever implement INotifyPropertyChanged ever again (unless you are in an environment where you can’t use <a href="http://castleproject.org/dynamicproxy/index.html">DynamicProxy</a>).&#160; It can automatically be done for you from now on.</p>
<p>The code is up on <a href="http://github.com/jonfuller/structuremap-autonotify">github</a>, and there is a gem up on <a href="http://rubygems.org/gems/structuremap.autonotify">rubygems</a> if you’re using <a href="http://nu.wikispot.org/">nu</a> or <a href="http://mattonrails.wordpress.com/2010/08/11/noodle-0-1-0/">noodle+bundler</a>.&#160; Fork it, send me a patch, use it, send feedback, etc.&#160; I hope you love it!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=fNeWCwaX89s:YbvEoJuw8JE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=fNeWCwaX89s:YbvEoJuw8JE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=fNeWCwaX89s:YbvEoJuw8JE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=fNeWCwaX89s:YbvEoJuw8JE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=fNeWCwaX89s:YbvEoJuw8JE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/fNeWCwaX89s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/08/27/never-implement-inotifypropertychanged-again/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/08/27/never-implement-inotifypropertychanged-again/</feedburner:origLink></item>
		<item>
		<title>Syntax highlighting for technical presentations</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/KcOTqKX6FFk/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/06/29/syntax-highlighting-for-technical-presentations/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 19:06:34 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[presenting]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/06/29/syntax-highlighting-for-technical-presentations/</guid>
		<description><![CDATA[Ever wanted a quick/easy/automated way to get syntax highlighted code from your editor of choice into PowerPoint? EVERY time I do a technical presentation I need this.&#160; Usually I resort to taking a screenshot, or finding an “export to html” type plugin for the editor I’m using at the time (Visual Studio, Vim, IntelliJ IDEA, [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted a quick/easy/automated way to get syntax highlighted code from your editor of choice into PowerPoint?</p>
<p>EVERY time I do a technical presentation I need this.&#160; Usually I resort to taking a screenshot, or finding an “export to html” type plugin for the editor I’m using at the time (Visual Studio, Vim, IntelliJ IDEA, etc.) and then try to get that somehow into my slides.</p>
<p>The problem I usually run into, is that while I like coding on a dark background with a 14pt font, that’s not usually that great for presentations.&#160; So I switch to my “presentation settings” that has a light background and bigger font size, and then switch back once I’m done taking screenshots or outputting to html. [ugh, what a pain… terribly manual].</p>
<p>Then what happens when I spot a bug in code that’s in PowerPoint, or I want to add a comment, or I need to change the syntax highlighting theme of the entire presentation because it doesn’t go well with the lighting in the room.&#160; UGH!&#160; What a disaster.</p>
<p>Basically, it’s always an uphill struggle, and it really bothered me… so I decided to fix it once and for all.</p>
<h3>Solution</h3>
<p>I’m going to solely discuss PowerPoint (for Windows or Mac) here.&#160; I don’t have a copy of iWork, and I’m too cheap to buy one.&#160; The reason I have Office for the Mac, is because I got it for free… KeyNote/Mac zealots: feel free to be zealous, but don’t hate on me because I’m frugal… unless you want to buy me a copy of iWork.</p>
<p>First, download: <a title="http://www.andre-simon.de/doku/highlight/en/highlight.html" href="http://www.andre-simon.de/doku/highlight/en/highlight.html">http://www.andre-simon.de/doku/highlight/en/highlight.html</a></p>
<h4>Windows Solution</h4>
<p>The highlight tool can output many formats, but the important one for Windows is RTF.</p>
<p>So something like:</p>
<p><font face="Lucida Console">highlight &lt; infile &gt; outfile –-rtf --syntax=rb&#160; --style=vim --font=”Lucida Console” --font-size=18</font></p>
<p>This will&#160; take the source code file ‘infile’ and syntax color it as Ruby to an RTF file and output it to ‘outfile’.&#160; The text will be 18pt Lucida Console and syntax highlighted with the “vim” color scheme.</p>
<p>There are lots of themes included, you can ask highlight for help (highlight --help), and it’ll tell you all the options available, as well as all the options available for output, and for languages that it supports.</p>
<p>Next, in PowerPoint, do “Insert Object” on your target slide, and choose “Create from file”.&#160; Make sure you check the “Link” checkbox before pressing OK.</p>
<p>Now, whenever your source changes, re-run your command line, then you can choose “Update Link” on your embedded object in PowerPoint, or if you close and then re-open PowerPoint, it’ll give you the option to update all your links.</p>
<h4>Mac Solution</h4>
<p>My version of PowerPoint for Mac doesn’t like being able to link to RTF files, but it does allow linking to images.</p>
<p>Highlight can output to SVG, but my PowerPoint doesn’t like that either.</p>
<p>Now download <a href="http://www.inkscape.org/">inkscape</a>, if you haven’t already.&#160; It will allow you to rasterize the SVG into a PNG, which PowerPoint does like.&#160; You can export your PNG from inkscape from the command line like this:</p>
<p><font face="Lucida Console">&lt;path to inkscape&gt; –export-png=&lt;png file&gt; &lt;svg file&gt;</font></p>
<p>The path to my inkscape command line runner is at:</p>
<p><font face="Lucida Console">/Applications/Inkscape.app/Contents/Resources/bin/inkscape</font></p>
<p>It turns out that inkscape doesn’t like external CSS files, which is what highlight gives you with your SVG, so you can merge your CSS file into your SVG file with a little script.&#160; The other cool bit about SVG, is you can tweak it, if you want, since it’s just XML.</p>
<p>Now, in PowerPoint, choose “Insert Picture” and browse to your newly generated PNG.&#160; Make sure the “Link to File” checkbox is checked.</p>
<p>I wrote a script to do this for the latest presentation I did on dependency injection.&#160; You can take a look at the script here [<a href="http://github.com/jonfuller/presentations/blob/master/dependencies/source.rb">source.rb</a>].&#160;&#160; It’s Ruby, and you can see where I merge the CSS and SVG files together, and where I mess with the line spacing as well (I wasn’t happy with the default line spacing, so I tweaked it to my preference).&#160; All the source/images/PowerPoint for that presentation are available here [<a href="http://github.com/jonfuller/presentations/tree/master/dependencies">dependencies presentation</a>] if you want to check them out to see how the whole process works.</p>
<p>Now, when I need to change the font and syntax theme throughout my entire presentation because it doesn’t match the lighting in the room, it’s super-simple: I change the configuration, re-run my script, update PowerPoint, and chill.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=KcOTqKX6FFk:2A4fIzh1h9Y:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=KcOTqKX6FFk:2A4fIzh1h9Y:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=KcOTqKX6FFk:2A4fIzh1h9Y:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=KcOTqKX6FFk:2A4fIzh1h9Y:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=KcOTqKX6FFk:2A4fIzh1h9Y:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/KcOTqKX6FFk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/06/29/syntax-highlighting-for-technical-presentations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/06/29/syntax-highlighting-for-technical-presentations/</feedburner:origLink></item>
		<item>
		<title>Rocket Surgery really is easy</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/q7_H9YP4-hw/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/06/24/rocket-surgery-really-is-easy/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 15:35:38 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[book review]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/06/24/rocket-surgery-really-is-easy/</guid>
		<description><![CDATA[I just finished reading Rocket Surgery Made Easy by Steve Krug (perhaps you know him from his other book Don’t Make Me Think).&#160; Here’s what I thought. What’s the point? Learn how to do usability testing yourself to gain most of the benefits of hiring someone to do it, and losing most of the negatives [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished reading <a href="http://www.amazon.com/Rocket-Surgery-Made-Easy-Yourself/dp/0321657292/ref=pd_sim_b_1">Rocket Surgery Made Easy</a> by <a href="http://www.sensible.com/">Steve Krug</a> (perhaps you know him from his other book <a href="http://www.amazon.com/Think-Common-Sense-Approach-Usability/dp/0789723107">Don’t Make Me Think</a>).&#160; Here’s what I thought.</p>
<p><strong>What’s the point? </strong>Learn how to do usability testing yourself to gain most of the benefits of hiring someone to do it, and losing most of the negatives (e.g. Big Honkin’ Report, $$$).&#160; The book’s other motive is to make sure you start doing some kind of usability testing.&#160; ALL of our sites/applications have usability problems.&#160; We could eliminate the big ones just by spending a little time on it.</p>
<p><strong>How was it? </strong>A pretty quick read.&#160; I’m a slow reader, and I made it through it in a couple hours a night for 3 nights.&#160; This thing is a prescriptive manual for conducting usability tests on a product you have (or on your competitors products, if you’d like to do that).&#160; It covers recruiting participants all the way to fixing the problems they discover.&#160; Usability testing doesn’t need to be a big production, hard to do, or scary.&#160; He lays it out step by step and give you (as the guy running the tests) guidance each step along the way, complete with checklists and scripts (I know, that sounds hokey, but I think it’ll actually work).</p>
<p><strong>Who should read it? </strong>If you’re reading this, you probably ought to read the book.&#160; Realistically, anyone remotely interested in having a usable application and is actually partly responsible for said application (PM, Tech Lead, Dev, Designer, Marketing, Tech Writing, Tester, etc.).&#160; Even if you aren’t going to be the one running/moderating the tests, it’s good to know what the participant’s are going through, what the moderator is doing behind the scenes, and what your role is as an observer.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=q7_H9YP4-hw:9tgR_0X99rs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=q7_H9YP4-hw:9tgR_0X99rs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=q7_H9YP4-hw:9tgR_0X99rs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=q7_H9YP4-hw:9tgR_0X99rs:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=q7_H9YP4-hw:9tgR_0X99rs:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/q7_H9YP4-hw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/06/24/rocket-surgery-really-is-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/06/24/rocket-surgery-really-is-easy/</feedburner:origLink></item>
		<item>
		<title>Teaching StructureMap About C# 4.0 Optional Parameters and Default Values</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/XQsnpWDwBtI/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/06/04/teaching-structuremap-about-c-4-0-optional-parameters-and-default-values/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 14:29:52 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[structuremap]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/06/04/teaching-structuremap-about-c-4-0-optional-parameters-and-default-values/</guid>
		<description><![CDATA[This week I ran into wanting to use C# 4.0 optional parameters, but wanted StructureMap (my IoC tool of choice) to respect the default value specified for those optional parameters. The Problem In this example, we’ll be pulling a command out of the container.&#160; The important part is the optional constructor parameter (level), and it’s [...]]]></description>
			<content:encoded><![CDATA[<p>This week I ran into wanting to use C# 4.0 optional parameters, but wanted <a href="http://structuremap.github.com/structuremap/index.html">StructureMap</a> (my IoC tool of choice) to respect the default value specified for those optional parameters.</p>
<h3>The Problem</h3>
<p>In this example, we’ll be pulling a command out of the container.&#160; The important part is the optional constructor parameter (<em>level</em>), and it’s default value (<em>Level.Info</em>).</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">LogCommand</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #ff6fb7">IDestination</span> _destination;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #d6adad">Level</span> _level;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> LogCommand(</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #ff6fb7">IDestination</span> destination, <span style="color: #d6adad">Level</span> level <span style="color: silver">=</span> <span style="color: #d6adad">Level</span><span style="color: silver">.</span>Info)</p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; _destination <span style="color: silver">=</span> destination;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; _level <span style="color: silver">=</span> level;</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #80ff00">/* logging code here */</span></p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>Here is your basic usage, but doesn’t work since StructureMap doesn’t know how to take advantage of the optional parameters with default values.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> container <span style="color: silver">=</span> <span style="color: #8ac5ff">new</span> <span style="color: #64b1ff">Container</span>(config <span style="color: silver">=&gt;</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; config<span style="color: silver">.</span>Scan(scanner <span style="color: silver">=&gt;</span></p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanner<span style="color: silver">.</span>TheCallingAssembly();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanner<span style="color: silver">.</span>AddAllTypesOf<span style="color: silver">&lt;</span><span style="color: #ff6fb7">IDestination</span><span style="color: silver">&gt;</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; scanner<span style="color: silver">.</span>WithDefaultConventions();</p>
<p style="margin: 0px">&#160;&#160;&#160; });</p>
<p style="margin: 0px">});</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> logCommand <span style="color: silver">=</span> container<span style="color: silver">.</span>GetInstance<span style="color: silver">&lt;</span><span style="color: #64b1ff">LogCommand</span><span style="color: silver">&gt;</span>();</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>The last line results in an exception because StructureMap doesn’t know how to fill in the <em>level </em>parameter.</p>
<h3>The Solution</h3>
<p>We can solve this by adding a new convention.&#160; One that adds information about default constructor arguments.&#160; Here is the implementation of the convention:</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">class</span> <span style="color: #64b1ff">DefaultCtorParameterConvention</span> : <span style="color: #ff6fb7">IRegistrationConvention</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">void</span> Process(<span style="color: #64b1ff">Type</span> type, <span style="color: #64b1ff">Registry</span> registry)</p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">if</span>(type<span style="color: silver">.</span>IsAbstract <span style="color: silver">||</span> type<span style="color: silver">.</span>IsEnum)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">return</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">var</span> ctor <span style="color: silver">=</span> type<span style="color: silver">.</span>GetGreediestCtor();</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">if</span>(<span style="color: silver">!</span>ctor<span style="color: silver">.</span>HasOptionalParameters())</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">return</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">var</span> inst <span style="color: silver">=</span> registry<span style="color: silver">.</span>For(type)<span style="color: silver">.</span>Use(type);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">foreach</span>(<span style="color: #8ac5ff">var</span> param <span style="color: #8ac5ff">in</span> ctor<span style="color: silver">.</span>GetOptionalParameters())</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; inst<span style="color: silver">.</span>Child(param<span style="color: silver">.</span>Name)<span style="color: silver">.</span>Is(param<span style="color: silver">.</span>DefaultValue);</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">}</p>
</p></div>
<p>Note: <em>GetGreediestCtor</em>, <em>HasOptionalParameters</em>, and <em>GetOptionalParameters </em>are extension methods.&#160; We’ll see their implementation shortly.</p>
<p>The convention inherits from the <em>IRegistrationConvention</em>, which is how you implement new conventions in StructureMap.&#160; It has only one method: <em>Process</em>.&#160; We filter out types that are abstract, are enums, or have constructors that don’t have optional parameters.&#160; Once we realize we have a constructor we want to deal with, we use the <em>Child </em>method, that sets either a property or a constructor argument (for our case, it’ll always be a constructor argument), and then we set it’s value to the parameter’s default value, as provided by the <em>ParameterInfo </em>object, for each optional parameter.</p>
<h4></h4>
<h4>Minor Details</h4>
<p>Curious about the implementation of <em>GetGreediestCtor </em>or the <em>*OptionalParameters </em>methods?&#160; If not, skip this section.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">static</span> <span style="color: #8ac5ff">bool</span> HasOptionalParameters(</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">this</span> <span style="color: #64b1ff">ConstructorInfo</span> ctor)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">return</span> ctor<span style="color: silver">.</span>GetOptionalParameters()<span style="color: silver">.</span>Any();</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">static</span> <span style="color: #ff6fb7">IEnumerable</span><span style="color: silver">&lt;</span><span style="color: #64b1ff">ParameterInfo</span><span style="color: silver">&gt;</span> GetOptionalParameters(<span style="color: #8ac5ff">this</span> <span style="color: #64b1ff">ConstructorInfo</span> ctor)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">return</span> ctor<span style="color: silver">.</span>GetParameters()<span style="color: silver">.</span>Where(</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; param <span style="color: silver">=&gt;</span> param<span style="color: silver">.</span>Attributes</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: silver">.</span>HasFlag(<span style="color: #d6adad">ParameterAttributes</span><span style="color: silver">.</span>Optional));</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">static</span> <span style="color: #64b1ff">ConstructorInfo</span> GetGreediestCtor(</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">this</span> <span style="color: #64b1ff">Type</span> target)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">return</span> target<span style="color: silver">.</span>GetConstructors()</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: silver">.</span>WithMax(ctor <span style="color: silver">=&gt;</span> ctor<span style="color: silver">.</span>GetParameters()<span style="color: silver">.</span>Length);</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">static</span> T WithMax<span style="color: silver">&lt;</span>T<span style="color: silver">&gt;</span>(</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">this</span> <span style="color: #ff6fb7">IEnumerable</span><span style="color: silver">&lt;</span>T<span style="color: silver">&gt;</span> target, <span style="color: #ffaeff">Func</span><span style="color: silver">&lt;</span>T, <span style="color: #8ac5ff">int</span><span style="color: silver">&gt;</span> selector)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">int</span> max <span style="color: silver">=</span> <span style="color: silver">-</span><span style="color: #ffff80">1</span>;</p>
<p style="margin: 0px">&#160;&#160;&#160; T currentMax <span style="color: silver">=</span> <span style="color: #8ac5ff">default</span>(T);</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">foreach</span>(<span style="color: #8ac5ff">var</span> item <span style="color: #8ac5ff">in</span> target)</p>
<p style="margin: 0px">&#160;&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">var</span> current <span style="color: silver">=</span> selector(item);</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">if</span>(current <span style="color: silver">&lt;=</span> max)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #8ac5ff">continue</span>;</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; max <span style="color: silver">=</span> current;</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; currentMax <span style="color: silver">=</span> item;</p>
<p style="margin: 0px">&#160;&#160;&#160; }</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">return</span> currentMax;</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>&#160;</p>
<h3>The Usage</h3>
<p>Here’s how to use your new convention.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 10pt">
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> container <span style="color: silver">=</span> <span style="color: #8ac5ff">new</span> <span style="color: #64b1ff">Container</span>(config <span style="color: silver">=&gt;</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160; config<span style="color: silver">.</span>Scan(scanner <span style="color: silver">=&gt;</span></p>
<p style="margin: 0px">&#160;&#160; {</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160; scanner<span style="color: silver">.</span>TheCallingAssembly();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160; scanner<span style="color: silver">.</span>AddAllTypesOf<span style="color: silver">&lt;</span><span style="color: #ff6fb7">IDestination</span><span style="color: silver">&gt;</span>();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160; scanner<span style="color: silver">.</span>WithDefaultConventions();</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160; scanner<span style="color: silver">.</span>Convention<span style="color: silver">&lt;</span><span style="color: #64b1ff">DefaultCtorParameterConvention</span><span style="color: silver">&gt;</span>();</p>
<p style="margin: 0px">&#160;&#160; });</p>
<p style="margin: 0px">});</p>
<p style="margin: 0px">&#160;</p>
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> logCommand <span style="color: silver">=</span> container<span style="color: silver">.</span>GetInstance<span style="color: silver">&lt;</span><span style="color: #64b1ff">LogCommand</span><span style="color: silver">&gt;</span>();</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>Now, when we pull the <em>LogCommand </em>out of the container, the <em>level </em>parameter gets defaulted to <em>Level.Info</em>, just like we specified in the constructor.&#160; Sweet!</p>
<h3></h3>
<h3>Conclusion</h3>
<p>This implementation is somewhat limiting, but the version I have in my <a href="http://github.com/jonfuller/StructureMapOptionalParams">github repo</a> is a little more open and configurable.&#160; It allows you to customize the instance key/name you use when registering your type, and also allows you to do additional, non-standard registrations if you need to.</p>
<p>Also, this doesn’t work if you’ve selected a constructor using the <em>SelectConstructor </em>config API from StructureMap, I’m not sure how to tap into that facility to look for that constructor rather than the greediest.</p>
<p>Am I missing something?&#160; Did something not make sense?&#160; Leave me a note!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=XQsnpWDwBtI:FSS6Gs2-sP8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=XQsnpWDwBtI:FSS6Gs2-sP8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=XQsnpWDwBtI:FSS6Gs2-sP8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=XQsnpWDwBtI:FSS6Gs2-sP8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=XQsnpWDwBtI:FSS6Gs2-sP8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/XQsnpWDwBtI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/06/04/teaching-structuremap-about-c-4-0-optional-parameters-and-default-values/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/06/04/teaching-structuremap-about-c-4-0-optional-parameters-and-default-values/</feedburner:origLink></item>
		<item>
		<title>Book Club: ProGit</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/DfJrs2mdvt4/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/05/28/book-club-progit/#comments</comments>
		<pubDate>Fri, 28 May 2010 14:41:48 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[book club]]></category>
		<category><![CDATA[book review]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/05/28/book-club-progit/</guid>
		<description><![CDATA[Book Club Foreword A couple years ago I brought up the idea of doing a book club here at SEP because I had participated in a couple before coming to SEP, but we called them SEDG (coined by Steve McConnell in Professional Software Development).&#160; I don’t really like that term, so we called it book [...]]]></description>
			<content:encoded><![CDATA[<h4>Book Club Foreword</h4>
<p>A couple years ago I brought up the idea of doing a book club here at SEP because I had participated in a couple <a href="http://jonfuller.codingtomusic.com/2008/05/16/why-i-quit-my-job/">before coming to SEP</a>, but we called them SEDG (coined by Steve McConnell in <a href="http://www.amazon.com/Professional-Software-Development-Schedules-Successful/dp/0321193679/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1275054384&amp;sr=1-1">Professional Software Development</a>).&#160; I don’t really like that term, so we called it book club instead.</p>
<p>We start up a new round a couple times a year, and small groups (3-8) break off and read different books (normally technical) that are interesting to them.&#160; We normally get together for an hour once a week to discuss/debate the chapter(s) we read for that week.&#160; I’ve participated in several including <a href="http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1275055151&amp;sr=1-1">DDD (Evans)</a>, <a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1275055177&amp;sr=1-1">Code Complete 2 (McConnell)</a>, <a href="http://www.amazon.com/Programming-Clojure-Pragmatic-Programmers-Halloway/dp/1934356336/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1275055194&amp;sr=1-1">Programming Clojure (Halloway)</a>, and now <a href="http://www.amazon.com/Pro-Git-Scott-Chacon/dp/1430218339/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1275055217&amp;sr=1-1">Pro Git (Chacon)</a>.&#160; It’s a lot of fun, and I always learn a lot about the content of the book, and about the people I participate with.</p>
<p>I’d highly recommend starting one at your company, or in your community, it’s a great way to connect with people and learn new things at the same time.</p>
<h4>The Book</h4>
<p>First off, the book is available for free, online at <a href="http://progit.org/">progit.org</a>.&#160; It’s also hosted in a repository on <a href="http://github.com/progit">github</a>, and there is an easy script to get it onto your kindle (with a little hacking of the script), so that was a win for me: <em>free </em>AND <em>kindle-ized</em>.</p>
<p>I participated with Matt (<a href="http://twitter.com/spraints">@spraints</a>) and Todd (<a href="http://twitter.com/snibble">@snibble</a>) (both of which know more about git than I do… Matt wrote <a href="http://github.com/spraints/git-tfs">git-tfs</a>, and Todd is working with git on a REAL project).</p>
<p>I’ll follow <a href="http://www.sep.com/ohri/2010/04/09/3q-book-review-linchpin/">Raman’s Recipe</a> to keep this short and simple.&#160; I’ll also mix-in the book club aspect to each section.</p>
<p><strong>What’s the point?&#160; </strong>Become a git ninja (or at least be able to play one <strike>on TV</strike> at work).&#160; Learn how to use git effectively, and give you the tools (ideas, knowledge, know-how) to give you a framework on how to effectively use it in your context.&#160; The cool thing about git, is that it gets out of your way and lets you work the way you and your team want to… not the other way around like SVN, TFS, P4, etc.&#160; It was also a great way to leech off of Matt and Todd’s knowledge.</p>
<p><strong>How was it?&#160; </strong>Great.&#160; Scott (author) certainly knows how to git down (I’ll be here all week <img src='http://jonfuller.codingtomusic.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ).&#160; I feel comfortable participating on a project that uses git (hey look, I am already!), and also feel comfortable making recommendations about using git (e.g. “You should use git!”), though, I do still get lost in hairy situations every once in awhile (thanks Matt for always saving me!).&#160; We had some good discussions on how it relates to us as company, and how we might use it.&#160; We also had some good discussions on schoolin’ me, so that was always good.&#160; The book contains lots of examples, with corresponding diagrams to help n00bs like myself understand what’s going on.</p>
<p><strong>Who should read it?&#160; </strong>Any developer planning on staying a developer for the next few years.&#160; DVCS seems to be on an up-tick, and I don’t really see it going away anytime soon (but hey, what do I know?).&#160; I don’t think you’ll be able to get by effectively in a DVCS world unless you’ve done at least some reading on the subject.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=DfJrs2mdvt4:ijhEDYuO5Tg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=DfJrs2mdvt4:ijhEDYuO5Tg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=DfJrs2mdvt4:ijhEDYuO5Tg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=DfJrs2mdvt4:ijhEDYuO5Tg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=DfJrs2mdvt4:ijhEDYuO5Tg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/DfJrs2mdvt4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/05/28/book-club-progit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/05/28/book-club-progit/</feedburner:origLink></item>
		<item>
		<title>IndyTechFest Recap</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/QBb_jZUbb-E/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/05/26/indytechfest-recap/#comments</comments>
		<pubDate>Wed, 26 May 2010 13:50:35 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[indytechfest]]></category>
		<category><![CDATA[people stuff]]></category>
		<category><![CDATA[presenting]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/05/26/indytechfest-recap/</guid>
		<description><![CDATA[IndyTechFest is teh awesome! I was privileged enough to be a speaker alongside some other ridiculously awesome regional speakers (and even had a few of them IN my presentation!)… star struck having @dburton, @timwingfield, @skimedic, and other community leaders like @myotherpants and @maggielongshore in the audience.&#160; I am truly humbled.&#160; Thanks to the organizers for [...]]]></description>
			<content:encoded><![CDATA[<p>IndyTechFest is teh awesome!</p>
<p>I was privileged enough to be a speaker alongside some other ridiculously awesome regional speakers (and even had a few of them IN my presentation!)… star struck having <a href="http://www.twitter.com/dburton">@dburton</a>, <a href="http://www.twitter.com/timwingfield">@timwingfield</a>, <a href="http://www.twitter.com/skimedic">@skimedic</a>, and other community leaders like <a href="http://www.twitter.com/myotherpants">@myotherpants</a> and <a href="http://www.twitter.com/maggielongshore">@maggielongshore</a> in the audience.&#160; I am truly humbled.&#160; Thanks to the organizers for allowing me to practice my teaching, and thanks to those that sat through my talk for supporting me (and also, thanks for the positive feedback from those I talked to afterwards!).&#160; [Side note: If you want to see my slides:&#160; <a title="http://github.com/jonfuller/presentations/tree/master/mongo/" href="http://github.com/jonfuller/presentations/tree/master/mongo/">http://github.com/jonfuller/presentations/tree/master/mongo/</a>]</p>
<p>The best part, however, had nothing do with any presentation at the event.&#160; It had to do with the amazing conversations we had in the <a href="http://en.wikipedia.org/wiki/Open_Space_Technology">Open Spaces</a> area hosted by <a href="http://www.twitter.com/alanstevens">@alanstevens</a>.&#160; I hung out there ALL day (except for when I was presenting), and got more out of any one open space topic than I have at entire conferences before.</p>
<p>We talked about Ruby on Rails, IronRuby, Public Speaking, Technical Speaking, Running a User Group, Linchpins, Alt.Net, Software Craftsmanship, DVCS (hg and git specifically)…    </p>
<p>I’ll pause and let that list of topics sink in.&#160; For me, that list is like a holy grail of a developer conference.</p>
<p>Most of this was deep discussion about the topic at hand by some great minds/leaders in the area.&#160; All of the aforementioned folks plus <a href="http://www.twitter.com/jayharris">@jayharris</a>, <a href="http://www.twitter.com/alanbarber">@alanbarber</a>, <a href="http://www.twitter.com/jademason">@jademason</a>, <a href="http://www.twitter.com/garrinmf">@garrinmf</a><a href="http://www.twitter.com/arktronic">,@arktronic</a>, <a href="http://www.twitter.com/browniepoints">@browniepoints</a> were together having these conversations, and it was really exciting (at least for me) hanging out with these guys and learning so much from them.</p>
<p>For me, this year, IndyTechFest was about the people and the interactions I had with them.&#160; What was it for you?</p>
<p>I can’t wait for next year.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=QBb_jZUbb-E:hZ-3IYao1j8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=QBb_jZUbb-E:hZ-3IYao1j8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=QBb_jZUbb-E:hZ-3IYao1j8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=QBb_jZUbb-E:hZ-3IYao1j8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=QBb_jZUbb-E:hZ-3IYao1j8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/QBb_jZUbb-E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/05/26/indytechfest-recap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/05/26/indytechfest-recap/</feedburner:origLink></item>
		<item>
		<title>Default Value</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/IkCaGFoOONg/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/05/11/default-value/#comments</comments>
		<pubDate>Tue, 11 May 2010 14:08:40 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/05/11/default-value/</guid>
		<description><![CDATA[In .NET we’ve got this cool little language construct called default, that’ll give you the default value for any given type.&#160; That is, null, for any reference type, or zero/false/DateTime.Min/etc. for value types. Here it is in action (nothing amazing going on here): var x = default(DateTime); So what if you don’t know the type [...]]]></description>
			<content:encoded><![CDATA[<p>In .NET we’ve got this cool little language construct called <em>default</em>, that’ll give you the default value for any given type.&#160; That is, null, for any reference type, or zero/false/DateTime.Min/etc. for value types.</p>
<p>Here it is in action (nothing amazing going on here):</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 13pt">
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> x <span style="color: silver">=</span> <span style="color: #8ac5ff">default</span>(<span style="color: #ffffaa">DateTime</span>);</p>
</p></div>
<p>So what if you don’t know the type you want the default of at compile time? You can’t say</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 13pt">
<div style="font-family: consolas; background: #101010; color: white; font-size: 13pt">
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> y <span style="color: silver">=</span> <span style="color: #8ac5ff">default</span>(today<span style="color: silver">.</span>GetType());</p>
</p></div>
</p></div>
<p>nor</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 13pt">
<p style="margin: 0px"><span style="color: #8ac5ff">var</span> z <span style="color: silver">=</span> <span style="color: #8ac5ff">typeof</span>(<span style="color: #ffffaa">DateTime</span>)<span style="color: silver">.</span><span style="color: red">GetDefault</span>();</p>
</p></div>
<p>That last one would be nice, but that “GetDefault” method doesn’t exist.</p>
<p>I’ve seen several solutions to this, that are basically variations on this theme:</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 13pt">
<p style="margin: 0px"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">static</span> <span style="color: #8ac5ff">object</span> GetDefaultValue(<span style="color: #64b1ff">Type</span> type)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px">&#160;&#160;&#160; <span style="color: #8ac5ff">return</span> type<span style="color: silver">.</span>IsValueType</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: silver">?</span> <span style="color: #64b1ff">Activator</span><span style="color: silver">.</span>CreateInstance(type)</p>
<p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: #8ac5ff">null</span>;</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>This certainly works, but I somehow feel like it’s not exactly perfect since it doesn’t use the <em>default </em>operator.</p>
<p>Here is how I normally do it.&#160; It sidesteps the IsValueType, and Activator stuff, and uses the built-in <em>default</em> language construct… First it grabs a handle to the <em>GetDefaultGeneric</em> method, and then makes the generic version of it with the specific type.&#160; Then it calls it, returning the value.</p>
<div style="font-family: consolas; background: #101010; color: white; font-size: 13pt">
<p style="margin: 0px" align="left"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">static</span> <span style="color: #8ac5ff">object</span> GetDefault(<span style="color: #8ac5ff">this</span> <span style="color: #64b1ff">Type</span> type)</p>
<p style="margin: 0px" align="left">{</p>
<p style="margin: 0px" align="left">&#160;&#160;&#160; <span style="color: #8ac5ff">var</span> getDefault <span style="color: silver">=</span> <span style="color: #8ac5ff">typeof</span>(<span style="color: #64b1ff">ExtReflection</span>)</p>
<p style="margin: 0px" align="left">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: silver">.</span>GetMethod(<span style="color: #ff8040">&quot;GetDefaultGeneric&quot;</span>);</p>
<p style="margin: 0px" align="left">&#160;&#160;&#160; <span style="color: #8ac5ff">var</span> typed <span style="color: silver">=</span> getDefault<span style="color: silver">.</span>MakeGenericMethod(type);</p>
<p style="margin: 0px" align="left">&#160;</p>
<p style="margin: 0px" align="left">&#160;&#160;&#160; <span style="color: #8ac5ff">return</span> typed<span style="color: silver">.</span>Invoke(<span style="color: #8ac5ff">null</span>, <span style="color: #8ac5ff">new</span> <span style="color: #8ac5ff">object</span>[] { });</p>
<p style="margin: 0px" align="left">}</p>
<p style="margin: 0px" align="left">&#160;</p>
<p style="margin: 0px" align="left"><span style="color: #8ac5ff">public</span> <span style="color: #8ac5ff">static</span> T GetDefaultGeneric<span style="color: silver">&lt;</span>T<span style="color: silver">&gt;</span>()</p>
<p style="margin: 0px" align="left">{</p>
<p style="margin: 0px" align="left">&#160;&#160;&#160; <span style="color: #8ac5ff">return</span> <span style="color: #8ac5ff">default</span>(T);</p>
<p style="margin: 0px" align="left">}</p>
<p style="margin: 0px">&#160;</p>
</p></div>
<p>Pretty simple, but something I’ve found useful every now and again.&#160; I’d guess this technique could be useful in other situations/contexts as well.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=IkCaGFoOONg:NLlBq-klO5Q:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=IkCaGFoOONg:NLlBq-klO5Q:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=IkCaGFoOONg:NLlBq-klO5Q:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=IkCaGFoOONg:NLlBq-klO5Q:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=IkCaGFoOONg:NLlBq-klO5Q:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/IkCaGFoOONg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/05/11/default-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/05/11/default-value/</feedburner:origLink></item>
		<item>
		<title>Book Review – Beautiful Teams</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/judNhvsCkwQ/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/04/26/book-review-beautiful-teams/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 13:38:12 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[book review]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/04/26/book-review-beautiful-teams/</guid>
		<description><![CDATA[I got beautiful teams a few months ago and tore into it hoping for lots of wonderful insights about how to help culture/foster/create functional and beautiful team environments. Instead, I ended up with a collection of stories from some people (some of which I've heard of, most of which I haven't) that have been on [...]]]></description>
			<content:encoded><![CDATA[<p>I got beautiful teams a few months ago and tore into it hoping for lots of wonderful insights ab<a href="http://jonfuller.codingtomusic.com/wp-content/uploads/2010/04/cat.gif"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="cat" border="0" alt="cat" align="right" src="http://jonfuller.codingtomusic.com/wp-content/uploads/2010/04/cat_thumb.gif" width="184" height="240" /></a>out how to help culture/foster/create functional and beautiful team environments. </p>
<p>Instead, I ended up with a collection of stories from some people (some of which I've heard of, most of which I haven't) that have been on teams and are willing to share their stories. </p>
<p>Now, I know all teams and projects and organizations are different, so maybe my expectation of 'Here is how you build a team.&#160; Step 1:...' was a little naive, but I certainly wasn't expecting what I got.&#160; There were some interesting stories throughout the book (i.e. a team of folks whose conference room ended up with one of the hijacked planes from 9/11 in it, literally, it crashed into their conference room), but I had few takeaways of solid advice on how or what I could do to help nurture a great team or start to build/rebuild a failing team. </p>
<p>All in all, I'd say there were some entertaining stories, but if you're looking for some concrete takeaway's, look elsewhere.&#160; And on that note, if you're looking for entertaining stories, you may be better off reading some Stephen King or something like that as well.&#160; Sorry O'Reilly (and Stellman and Greene, also authors of Head First C#, which I do have and enjoyed thoroughly), this just wasn't for me.&#160; Probably my own fault for having false expectations.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=judNhvsCkwQ:6Z_UsV9v4aw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=judNhvsCkwQ:6Z_UsV9v4aw:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=judNhvsCkwQ:6Z_UsV9v4aw:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=judNhvsCkwQ:6Z_UsV9v4aw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=judNhvsCkwQ:6Z_UsV9v4aw:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/judNhvsCkwQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/04/26/book-review-beautiful-teams/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/04/26/book-review-beautiful-teams/</feedburner:origLink></item>
		<item>
		<title>The file exists</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/RjfZ2zq8Cy4/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/04/12/the-file-exists/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 11:57:01 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dev]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/04/12/the-file-exists/</guid>
		<description><![CDATA[Maybe I should title this post ‘How to write a better error message’ instead. I just went on a wild goose chase trying to find the solution to an error I kept seeing in SQL Server Management Studio every time I tried to run a query or open a table for editing (or anything else [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe I should title this post ‘How to write a better error message’ instead.</p>
<p>I just went on a wild goose chase trying to find the solution to an error I kept seeing in SQL Server Management Studio every time I tried to run a query or open a table for editing (or anything else that would end up executing a SQL query).&#160; The error message?</p>
<blockquote><p>The file exists</p>
</blockquote>
<p>Not exactly useful.</p>
<p>It turns out my temp directory was ‘full’.&#160; The message sort of makes sense, now that I know what the problem was, but it isn’t really possible to draw the correct conclusion from that error message.</p>
<p>So two takeaways (for me):</p>
<ol>
<li>Craft error messages carefully.&#160; The whole reason for showing/logging error messages in the first place is to create some kind of traceability to a problem.&#160; The information contained in an error message should lead the user to a better understanding of the problem and possibly an indication of if they might be able to fix it. </li>
<li><em>The file exists</em> means <em>clear your temp directory first, before chasing that goose</em>. </li>
</ol>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=RjfZ2zq8Cy4:qZ5NyV9vRQE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=RjfZ2zq8Cy4:qZ5NyV9vRQE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=RjfZ2zq8Cy4:qZ5NyV9vRQE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=RjfZ2zq8Cy4:qZ5NyV9vRQE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=RjfZ2zq8Cy4:qZ5NyV9vRQE:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/RjfZ2zq8Cy4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/04/12/the-file-exists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/04/12/the-file-exists/</feedburner:origLink></item>
		<item>
		<title>Managing Distractions</title>
		<link>http://feedproxy.google.com/~r/jonfuller/~3/wd2zqpSAuJI/</link>
		<comments>http://jonfuller.codingtomusic.com/2010/02/24/managing-distractions/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 18:32:39 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonfuller.codingtomusic.com/2010/02/24/managing-distractions/</guid>
		<description><![CDATA[I’ve recently started to practice the pomodoro technique.&#160; Holy F!&#160; This super simple practice has uncovered how much time I completely waste in a workday (not to mention my non-work time). I feel like I’m cheating myself and my customer (read employer), so I’m going to do something about it.&#160; I’ve never read much of [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve recently started to practice the <a href="http://www.pomodorotechnique.com/">pomodoro</a> <a href="http://www.pomodorotechnique.com/">technique</a>.&#160; Holy F!&#160; This super simple practice has uncovered how much time I <strong>completely</strong> waste in a workday (not to mention my non-work time).</p>
<p>I feel like I’m cheating myself and my <em>customer</em> (<em>read employer</em>), so I’m going to do something about it.&#160; I’ve never read much of <a href="https://www.stephencovey.com/">Covey</a>, but <a href="http://twitter.com/shanselman">@shanselman</a> mentioned Covey’s quadrants (see a mind-map version below) in this <a href="http://vimeo.com/7680468">video</a> I watched the other day.&#160; For me, living only in quadrants 1 &amp; 2 (the top two) is all good, but there are times when I basically want to just ‘check out’ and be a bum (Office Space style).&#160; My goal is to be intentional and choose when I’m doing that, rather than just letting it happen.</p>
<p><a href="http://mindmapblog.wordpress.com/2008/06/21/stephen-coveys-time-management-matrix-in-a-mindmap/"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="time-management-1" border="0" alt="time-management-1" src="http://jonfuller.codingtomusic.com/wp-content/uploads/2010/02/timemanagement1.jpg" width="467" height="386" /></a> </p>
<h4>Baby Steps</h4>
<p>The pomodoro technique is a great start to help manage and become aware, but I need some help.&#160; Here’s what I set up for myself at this point:</p>
<ul>
<li><a href="http://github.com/jonfuller/pomodo7o">Pomodo7o</a> with Growl Plugin </li>
<li><a href="http://www.growlforwindows.com/gfw/">Growl</a> using the Visor notification (I don’t like toast, so this is great for me) </li>
<li><a href="http://virtuawin.sourceforge.net/">VirtuaWin</a> (for Windows, this is built into most other OS’s, e.g. Spaces on OSX) </li>
<li>Throw ALL distracting things to a second VirtuaWin workspace.
<ul>
<li>Gmail </li>
<li>GReader </li>
<li>Twitter (turn off twitter notifications) </li>
<li>Outlook (turn off new mail toast and new mail tray indicator) </li>
<li><a href="http://www.evernote.com/">Evernote</a> (turn off any messages) </li>
<li>Windows Live Writer </li>
<li>Time tracking applications </li>
<li>Anything else that dings/hoots/toasts or otherwise distracts you </li>
</ul>
</li>
<li>Only check email (Gmail <strong>and </strong>Outlook) during pomodoro breaks or other scheduled times </li>
<li>Only check twitter during pomodoro breaks or other scheduled times (I’ve started only checking twitter before I start work, and right before I head home). </li>
<li>Hook <a href="http://www.growlforwindows.com/plugins/outlook">Outlook into Growl</a> so I don’t miss meetings </li>
<li>Keep a text file or post-it note ready to capture interruptions.&#160; Most interruptions can be logged, and then dealt with in a subsequent pomodoro. </li>
</ul>
<p>I only allow myself to go over to my other desktop during a pomodoro break.&#160; This makes it really easy to know when I’m going off task.&#160; It also makes it REALLY hard to find something wasteful to do.&#160; There isn’t anything in my “workspace” that is wasteful anymore, so if I do ANYTHING in there, it’ll probably be productive.</p>
<h4>Feeling Too Disconnected?</h4>
<p>I’ve got the shakes!&#160; Doing this thing cold turkey is hella hard.&#160; So here’s a patch to wean you off.</p>
<p>Download <a href="http://www.mageuzi.com/trowl/">Trowl</a> and hook up @’s and DM’s (and select other folks you might need to hear from throughout the day).&#160; Now those tweets will be Growled at you.&#160; I don’t get @’ed or DM’ed too often, so it’s not terribly distracting, but has high reward in helping to reduce the withdrawal symptoms.</p>
<p>&#160;</p>
<p>I’m just starting, so this is my first attempt really.&#160; How do you manage?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/jonfuller?a=wd2zqpSAuJI:XqDsbog1Wj0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/jonfuller?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=wd2zqpSAuJI:XqDsbog1Wj0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=wd2zqpSAuJI:XqDsbog1Wj0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/jonfuller?a=wd2zqpSAuJI:XqDsbog1Wj0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/jonfuller?i=wd2zqpSAuJI:XqDsbog1Wj0:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/jonfuller/~4/wd2zqpSAuJI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jonfuller.codingtomusic.com/2010/02/24/managing-distractions/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://jonfuller.codingtomusic.com/2010/02/24/managing-distractions/</feedburner:origLink></item>
	</channel>
</rss>

