<?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>The Inquisitive Coder - Davy Brion's Blog</title>
	
	<link>http://davybrion.com/blog</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Sun, 08 Nov 2009 12:17:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/davybrion" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Request/Response Service Layer: Requests And Responses</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/Nn-7dazqcEo/</link>
		<comments>http://davybrion.com/blog/2009/11/requestresponse-service-layer-requests-and-responses/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 12:17:17 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Request/Response Service Layer]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1796</guid>
		<description><![CDATA[Note: This post is part of a series. Be sure to read the introduction here.
In a typical WCF service, you&#8217;ll have one or more Service Contracts, and each Service Contract will define one or more Service Operations.  These operations are just methods that you can call on the service.  An operation can have [...]]]></description>
			<content:encoded><![CDATA[<p>Note: This post is part of a series. Be sure to read the introduction <a href="http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/">here</a>.</p>
<p>In a typical WCF service, you&#8217;ll have one or more Service Contracts, and each Service Contract will define one or more Service Operations.  These operations are just methods that you can call on the service.  An operation can have parameters and it can have a return value.  Most people typically expose multiple operations on their services for each piece of functionality they want their service to offer to consumers.</p>
<p>The Request/Response Service Layer (RRSL) takes a different approach.  It has one service contract, with only one operation defined:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IRequestProcessor</span> : <span style="color: #2b91af;">IDisposable</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Response</span>[] Process(<span style="color: blue;">params</span> <span style="color: #2b91af;">Request</span>[] requests);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Notice the lack of the typical WCF attributes such as ServiceContract and OperationContract.  The RRSL is actually completely independent from WCF and only uses WCF as a transport medium.  That&#8217;s the topic of another post though, so let&#8217;s get back to the topic at hand: requests and responses.</p>
<p>As you can see, the Process method receives an array of Request objects and returns an array of Response objects.  So what exactly is a Request and what is a Response?  This is what they look like:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Request</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Response</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ExceptionInfo</span> Exception { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ExceptionType</span> ExceptionType { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Both are classes that you should derive specific Request and Response types from.  The Response class automatically comes with an ExceptionInfo class (which is pretty much identical to WCF&#8217;s ExceptionDetail class and only exists because Silverlight 2 did not support ExceptionDetail) and an ExceptionType enum:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">enum</span> <span style="color: #2b91af;">ExceptionType</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; None,</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Business,</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Security,</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EarlierRequestAlreadyFailed,</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Unknown</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>The ExceptionInfo class (or ExceptionDetail if you would prefer to use that) does not contain the original Exception object, so the ExceptionType enumeration can be used to handle each type of exception differently on the client-side.  Obviously, if a Request was handled without problems, the ExceptionInfo property of its Response will be null, and the ExceptionType property will be set to ExceptionType.None.</p>
<p>The idea is basically that instead of defining operations on your services, you create a specific Request class and a corresponding Response class for each operation that you would normally expose on your service.  The request type <em>is</em> the operation, its properties are the operation&#8217;s parameters and the Response type is the operation&#8217;s return value.  Note that you can define multiple properties on your Response types, effectively giving you the ability to support multiple return values for an operation without having to use out parameters.</p>
<p>A simple example of a Request and its Response can look like this:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">GetProductOverviewsRequest</span> : <span style="color: #2b91af;">Request</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> NamePattern { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span>? CategoryId { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span>? SupplierId { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">GetProductOverviewsResponse</span> : <span style="color: #2b91af;">Response</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ProductOverviewDto</span>[] Products { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Note that by default, if you&#8217;re using at least .NET 3.5 SP1, you do not need to mark your Request/Response types as Serializable or apply the DataContract/DataMember attributes on them <em>if</em> the assembly containing the Request/Response types can be shared between both your service and your clients.  If your service needs to be interoperable with other platforms or you can&#8217;t share the assembly containing the types, then you obviously do need to use the DataContract/DataMember attributes but if you&#8217;re only communicating with .NET clients and you can share the assembly, it&#8217;s not needed.  Provided that all of the clients also use at least .NET 3.5 SP1 obviously.</p>
<p>At this point you might be wondering: &#8220;what exactly is the benefit of doing this?&#8221;.  Well, at this point in the series there is only one benefit that you can see already: the fact that the Process method of the service receives <em>an array</em> of requests and returns <em>an array</em> of corresponding responses.  That means that you can easily send multiple requests to the service with only one roundtrip, which can greatly reduce the network communication overhead that you can otherwise suffer with typical Service Contracts, especially if their interface is too chatty.</p>
<p>There are many more benefits to using the RRSL, but you&#8217;ll have to read the rest of the posts in the series to learn about those <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/4B2TV3mXiExedom-bvMkq9tv2Ds/0/da"><img src="http://feedads.g.doubleclick.net/~a/4B2TV3mXiExedom-bvMkq9tv2Ds/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/4B2TV3mXiExedom-bvMkq9tv2Ds/1/da"><img src="http://feedads.g.doubleclick.net/~a/4B2TV3mXiExedom-bvMkq9tv2Ds/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=Nn-7dazqcEo:ViPigVC80R0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=Nn-7dazqcEo:ViPigVC80R0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=Nn-7dazqcEo:ViPigVC80R0:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/Nn-7dazqcEo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/requestresponse-service-layer-requests-and-responses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/11/requestresponse-service-layer-requests-and-responses/</feedburner:origLink></item>
		<item>
		<title>Request/Response Service Layer Series</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/kWX9uK70Uno/</link>
		<comments>http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 12:16:38 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Request/Response Service Layer]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1794</guid>
		<description><![CDATA[I&#8217;ve blogged about the Request/Response Service Layer (which i&#8217;ll just abbreviate to RRSL from now on) several times already, but i wanted to group everything there is to know about the RRSL together in a series of posts that should cover everything comprehensively, yet very in-depth.
I wrote a post a couple of months ago to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve blogged about the Request/Response Service Layer (which i&#8217;ll just abbreviate to RRSL from now on) several times already, but i wanted to group everything there is to know about the RRSL together in a series of posts that should cover everything comprehensively, yet very in-depth.</p>
<p>I wrote a <a href="http://davybrion.com/blog/2009/07/why-i-dislike-classic-or-typical-wcf-usage/">post</a> a couple of months ago to discuss what i dislike about typical/classic WCF services and how the RRSL solves many of those issues.  If you haven&#8217;t read that post yet, be sure that you do because i&#8217;d rather not repeat the thoughts from that post (yet again <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) and it&#8217;ll definitely help you understand some of the choices and decisions that influenced the RRSL.</p>
<p>These are the posts that this series consists of:</p>
<ol>
<li><a href="http://davybrion.com/blog/2009/11/requestresponse-service-layer-requests-and-responses/">Requests And Responses</a></li>
<li>Processing Requests</li>
<li>Handling Requests</li>
<li>Exposing The Service Layer Through WCF</li>
<li>Synchronous Client-Side Usage</li>
<li>Testing Synchronous Client-Side Usage</li>
<li>Asynchronous Client-Side Usage</li>
<li>Testing Asynchronous Client-Side Usage</li>
<li>Conclusions</li>
</ol>
<p>Note: the links to the posts will be updated as the posts are scheduled.</p>
<p>Note: All of the code shown in this series may be reused under the terms of the <a href="http://creativecommons.org/licenses/by/2.0/">Creative Commons Attribution 2.0 License</a>.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/CengNcPw9i7I4PEYUyA3L5dKZWY/0/da"><img src="http://feedads.g.doubleclick.net/~a/CengNcPw9i7I4PEYUyA3L5dKZWY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/CengNcPw9i7I4PEYUyA3L5dKZWY/1/da"><img src="http://feedads.g.doubleclick.net/~a/CengNcPw9i7I4PEYUyA3L5dKZWY/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=kWX9uK70Uno:Iv5of3ztb0M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=kWX9uK70Uno:Iv5of3ztb0M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=kWX9uK70Uno:Iv5of3ztb0M:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/kWX9uK70Uno" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/</feedburner:origLink></item>
		<item>
		<title>Why I Don’t Use Twitter</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/Zhy6l9ADBYM/</link>
		<comments>http://davybrion.com/blog/2009/11/why-i-dont-use-twitter/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 14:31:31 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Opinions]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1870</guid>
		<description><![CDATA[I routinely keep an eye on the search engine keywords that are used by people when they make it to this site through google or bing (in a whopping 3.7% of the cases).  Lately, i&#8217;ve noticed an increase in people googling (or binging) for some kind of combination of &#8216;davy brion&#8217; and &#8216;twitter&#8217; which, [...]]]></description>
			<content:encoded><![CDATA[<p>I routinely keep an eye on the search engine keywords that are used by people when they make it to this site through google or bing (in a whopping 3.7% of the cases).  Lately, i&#8217;ve noticed an increase in people googling (or binging) for some kind of combination of &#8216;davy brion&#8217; and &#8216;twitter&#8217; which, i presume, is to find my twitter page.  Unfortunately (or luckily) for those people, i don&#8217;t use twitter.  Never have and i probably never will.</p>
<p>I don&#8217;t really have a problem with Twitter, but it&#8217;s just not right for me.  For one, my opinions and statements often need quite a bit of clarification and context if i don&#8217;t want them to be misconstrued.  Hell, i often write longish posts to clarify them and even then there are still quite a few people who still misinterpret things.  If i&#8217;d post them in tiny blurbs on Twitter, i&#8217;d probably end up wasting quite a bit of time in follow-up discussions to try to clear things up.  So when it comes to communicating <em>my thoughts</em>, it&#8217;s definitely not right for me.</p>
<p>When it comes to following <em>other peoples&#8217; thoughts</em>, the situation is even worse IMO.  I looked over the twitter pages of a number of developers who&#8217;s blogs i read and enjoy and frankly, the occasionally interesting tweets are far, far outnumbered by a lot of stuff that i&#8217;m simply not interested in.  I honestly don&#8217;t care about your personal life unless you&#8217;re a friend of mine.  I don&#8217;t need to know how much you hated being stuck in traffic this morning (and why on earth would i want to look at a picture of it?).  I don&#8217;t need to know how great the date was you had last night.  I don&#8217;t need to know how much you enjoyed the latest episode of House M.D.  I don&#8217;t need to know what album or song you&#8217;re enjoying at the moment.  I don&#8217;t need to know about the crappy meeting you just had or the fact that you didn&#8217;t get what you wanted from it.  I certainly don&#8217;t need to know about the mess your cat left next to your keyboard and i really, really don&#8217;t need to know about your bowl movements either.  I&#8217;m not really interested in all of the sucking-up-to-the-cool-kids-while-they-pretend-not-to-care either, we are after all no longer in high school.</p>
<p>It&#8217;s too bad really, because the interesting tweets that you do come across can definitely be worthy of discussion or conversation.  But twitter has got to have the lowest signal-to-noise ratio that i&#8217;ve ever seen on the internet and i hardly have enough time to work on the things that i want to work on, let alone spending time going through a bunch of tweets every day to find the occasionally good one.  Again, that&#8217;s just my personal opinion.  If you use and enjoy twitter, that&#8217;s great! But it&#8217;s just not for me.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/T9hy1mO2hCWqGxAdtsZfsHpzUT8/0/da"><img src="http://feedads.g.doubleclick.net/~a/T9hy1mO2hCWqGxAdtsZfsHpzUT8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/T9hy1mO2hCWqGxAdtsZfsHpzUT8/1/da"><img src="http://feedads.g.doubleclick.net/~a/T9hy1mO2hCWqGxAdtsZfsHpzUT8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=Zhy6l9ADBYM:b_DdCXWd6tg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=Zhy6l9ADBYM:b_DdCXWd6tg:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=Zhy6l9ADBYM:b_DdCXWd6tg:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/Zhy6l9ADBYM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/why-i-dont-use-twitter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/11/why-i-dont-use-twitter/</feedburner:origLink></item>
		<item>
		<title>Integrated Security With Silverlight And WCF</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/XdPfs9UqT1c/</link>
		<comments>http://davybrion.com/blog/2009/11/integrated-security-with-silverlight-and-wcf/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 11:41:32 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1856</guid>
		<description><![CDATA[I lost some time yesterday trying to get a Silverlight client to use Integrated Security with a WCF service so i figured i&#8217;d post the steps necessary to make it work here.
First of all, you need to make sure that your IIS installation has support for Windows Authentication.  Go to Add/Remove Programs (appwiz.cpl), click [...]]]></description>
			<content:encoded><![CDATA[<p>I lost some time yesterday trying to get a Silverlight client to use Integrated Security with a WCF service so i figured i&#8217;d post the steps necessary to make it work here.</p>
<p>First of all, you need to make sure that your IIS installation has support for Windows Authentication.  Go to Add/Remove Programs (appwiz.cpl), click on Turn Windows Features on or off, select Internet Information Services &#8211; World Wide Web Services &#8211; Security and make sure that Windows Authentication is checked.</p>
<p>Next, you need to make sure that the virtual directory where you&#8217;re hosting the WCF service has Windows Authentication enabled.  Open Internet Information Services Manager (inetmgr), select the virtual directory where the WCF service is hosted, click on the Authentication icon and enable Windows Authentication. </p>
<p>After that, you need to add the following to the binding configuration of the service endpoint (in the host, obviously):</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">TransportCredentialOnly</span>&quot;<span style="color: blue;">&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">transport</span><span style="color: blue;"> </span><span style="color: red;">clientCredentialType</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Windows</span>&quot;<span style="color: blue;"> /&gt;</span></p>
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/</span><span style="color: #a31515;">security</span><span style="color: blue;">&gt;</span></p>
</div>
<p></code></p>
<p>I only got it working with basicHttpBinding, so unfortunately i can no longer use the customBinding to use binary XML&#8230;</p>
<p>In your Silverlight project, open the ServiceReferences.ClientConfig file and add the following to the binding configuration:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;</span><span style="color: #a31515;">security</span><span style="color: blue;"> </span><span style="color: red;">mode</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">TransportCredentialOnly</span>&quot;<span style="color: blue;"> /&gt;</span></p>
</div>
<p></code></p>
<p>After that, you should be able to do this in your WCF service:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">WindowsIdentity</span> myuser = <span style="color: #2b91af;">ServiceSecurityContext</span>.Current.WindowsIdentity;</p>
</div>
<p></code></p>
<p>And that should return the Windows user of the user running the Silverlight client.</p>
<p>For the record: this is with Silverlight 3&#8230; i have no idea if it&#8217;ll work with Silverlight 2</p>

<p><a href="http://feedads.g.doubleclick.net/~a/Aq3iw6cguKdBg6DACz4rW1TqshM/0/da"><img src="http://feedads.g.doubleclick.net/~a/Aq3iw6cguKdBg6DACz4rW1TqshM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Aq3iw6cguKdBg6DACz4rW1TqshM/1/da"><img src="http://feedads.g.doubleclick.net/~a/Aq3iw6cguKdBg6DACz4rW1TqshM/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=XdPfs9UqT1c:F7PAPNoGvQY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=XdPfs9UqT1c:F7PAPNoGvQY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=XdPfs9UqT1c:F7PAPNoGvQY:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/XdPfs9UqT1c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/integrated-security-with-silverlight-and-wcf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/11/integrated-security-with-silverlight-and-wcf/</feedburner:origLink></item>
		<item>
		<title>Constructor Injection vs Setter Injection</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/olYPQnVM97c/</link>
		<comments>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 05:00:31 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Dependency Injection]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715</guid>
		<description><![CDATA[For those of you who&#8217;ve used Dependency Injection, you know that the two most common ways of injecting a dependency into a class are constructor injection and setter injection.  For those of you who haven&#8217;t used Dependency Injection yet, here&#8217;s a simple example which shows both techniques:


.cf { font-family: Consolas; font-size: 9pt; color: black; [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you who&#8217;ve used Dependency Injection, you know that the two most common ways of injecting a dependency into a class are constructor injection and setter injection.  For those of you who haven&#8217;t used Dependency Injection yet, here&#8217;s a simple example which shows both techniques:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: green; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">MyService</span> : <span class="cb2">IMyService</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb2">IRequiredDependency</span> requiredDependency;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb2">IOptionalDependency</span> OptionalDependency { <span class="cb1">get</span>; <span class="cb1">set</span>; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> MyService(<span class="cb2">IRequiredDependency</span> requiredDependency)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">this</span>.requiredDependency = requiredDependency;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">void</span> DoSomething()</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb3">// do something cool and/or important</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb3">// ...</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>This example is very abstract, but it should be pretty clear.  Constructor injection is used to inject the required dependency whenever an instance of MyService is created, whereas setter injection is used to inject the optional dependency <strong>after</strong> the instance is created.  I obviously can&#8217;t speak for everyone who uses Dependency Injection, but generally speaking most people use constructor injection for required dependencies and setter injection for optional dependencies.</p>
<p>There is however one situation in which i prefer setter injection for required dependencies over constructor injection: dependencies of abstract classes or base classes.  For instance, in our service layer each incoming request is handled by a specific RequestHandler.  Most of our RequestHandlers need our NHibernate infrastructure to be set up, which is automatically taken care of by our UnitOfWork implementation.  So we have the following NhRequestHandler class (simplified for the purpose of this blog post):</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: green; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">abstract</span> <span class="cb1">class</span> <span class="cb2">NhRequestHandler</span>&lt;TRequest, TResponse&gt; : <span class="cb2">RequestHandler</span>&lt;TRequest, TResponse&gt;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">where</span> TRequest : <span class="cb2">Request</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">where</span> TResponse : <span class="cb2">Response</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb2">IUnitOfWork</span> UnitOfWork { <span class="cb1">get</span>; <span class="cb1">set</span>; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">override</span> <span class="cb2">Response</span> Handle(<span class="cb2">Request</span> request)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">using</span> (<span class="cb2">ITransaction</span> transaction = UnitOfWork.CreateTransaction())</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Response</span> response;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">try</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; response = <span class="cb1">base</span>.Handle(request); <span class="cb3">// calls the specific Handle(TRequest) method of the derived handler</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; transaction.Commit();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">catch</span> (<span class="cb2">Exception</span> e)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; transaction.Rollback();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">throw</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> response;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code>  </p>
<p>As you can see, the IUnitOfWork dependency is a required dependency because you would get a NullReferenceException when trying to handle a request without having a IUnitOfWork instance present.  Yet, i really don&#8217;t want to put it in the constructor because then each and every RequestHandler that derives from this will also have to put it in the constructor, even though most of them won&#8217;t access the IUnitOfWork directly.</p>
<p>Actually, most of our applications inherit from the NhRequestHandler and then add some more dependencies that some kind of base BusinessRequestHandler will need.  These are dependencies to deal with authentication, authorization, user context, application context, etc&#8230; Some of these dependencies will be used by the derived RequestHandlers, some won&#8217;t.  All of them however will indeed be used by the BusinessRequestHandler so they are definitely required dependencies.  Using constructor injection for these dependencies would lead to &#8216;noise&#8217; in every derived RequestHandler&#8217;s constructor.</p>
<p>Instead, we use setter injection for all of a base-type&#8217;s dependencies, and use constructor injection only for the dependencies of the derived types.  It keeps the constructors as clean as they can be and avoids unnecessary noise.  We know that our IOC container will fulfill all the constructor dependencies as well as each property dependency in the inheritance hierarchy so there&#8217;s no chance of anything going wrong there.  Unless of course somebody seriously breaks the IOC configuration but in that case, our applications won&#8217;t even make it through the simplest of requests so that&#8217;s not something that will ever happen unnoticed.</p>
<p>And for our tests, we always inherit our fixtures from things like HandlerTest or ControllerTest or whatever where all of those property dependencies are automatically set up with mocks or stubs, so it doesn&#8217;t really cause problems there either.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/Sda8qI0JOuaY-Az5wcxpaaIWb-c/0/da"><img src="http://feedads.g.doubleclick.net/~a/Sda8qI0JOuaY-Az5wcxpaaIWb-c/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Sda8qI0JOuaY-Az5wcxpaaIWb-c/1/da"><img src="http://feedads.g.doubleclick.net/~a/Sda8qI0JOuaY-Az5wcxpaaIWb-c/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=olYPQnVM97c:pqc8AxFAxQA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=olYPQnVM97c:pqc8AxFAxQA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=olYPQnVM97c:pqc8AxFAxQA:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/olYPQnVM97c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/</feedburner:origLink></item>
		<item>
		<title>New Hardware, New Software</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/MHGLRBV0Gy8/</link>
		<comments>http://davybrion.com/blog/2009/11/new-hardware-new-software/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 14:52:23 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Off Topic]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1833</guid>
		<description><![CDATA[I bought a new iMac last week (the 21.5&#8243; since the 27&#8243; is just a bit too over the top for my taste  ) to replace my aging MacBook.  Performance of the machine is very nice and in general it&#8217;s just a great system.  The screen is fantastic as well and i&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I bought a new iMac last week (the 21.5&#8243; since the 27&#8243; is just a bit too over the top for my taste <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) to replace my aging MacBook.  Performance of the machine is <strong>very</strong> nice and in general it&#8217;s just a great system.  The screen is fantastic as well and i&#8217;m definitely loving the resolution (1920*1080).  The new Magic Mouse is excellent, though i do miss the middle-click (which i used to use extensively to open links on sites in new tabs).</p>
<p>But the best part of it is that i also upgraded to the newly released <a href="http://www.vmware.com/products/fusion/">VMWare Fusion 3</a>.  As some of you already know, i use my Mac for all regular computer usage, and i use a virtual machine to run Windows which i only use when i&#8217;m doing .NET development at home.  I was still using a virtualized Windows XP on my MacBook because for one, i can&#8217;t stand Vista and i figured it would be way too slow to run in a virtual machine on the MacBook anyway.  But for the past 2 months or so, i&#8217;ve been using Windows 7 at work and much to my surprise, i&#8217;m actually very happy with it.  It&#8217;s still butt-ugly, but at least it runs nicely, it&#8217;s fast, it&#8217;s stable, and it hardly ever annoys me.  It&#8217;s like Windows 2000 all over again :p.  So i figured it was time to make the switch at home as well.  So now i have a virtual Windows 7 with Visual Studio 2008 running on the iMac (only when i wanna do .NET stuff obviously) and it&#8217;s running very well.  Performance is really unbelievable, considering that it&#8217;s virtualized and i only gave the VM 2GB of RAM.  I did disable Aero (i could use it, but the difference was noticeable) but i kept the typical new Windows look, if only because the classic look is even worse.</p>
<p>While writing code at home had become more of a chore than a joy because of the slowness of running a virtual Windows XP on the MacBook, it&#8217;s now really enjoyable again <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/lpgFi5TJARWkB_PbJEZjZfT-usA/0/da"><img src="http://feedads.g.doubleclick.net/~a/lpgFi5TJARWkB_PbJEZjZfT-usA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/lpgFi5TJARWkB_PbJEZjZfT-usA/1/da"><img src="http://feedads.g.doubleclick.net/~a/lpgFi5TJARWkB_PbJEZjZfT-usA/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=MHGLRBV0Gy8:3yhuAKFW2lo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=MHGLRBV0Gy8:3yhuAKFW2lo:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=MHGLRBV0Gy8:3yhuAKFW2lo:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/MHGLRBV0Gy8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/11/new-hardware-new-software/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/11/new-hardware-new-software/</feedburner:origLink></item>
		<item>
		<title>My Top 10 Personal Favorite And Overlooked Posts</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/v5yCDAWRbvw/</link>
		<comments>http://davybrion.com/blog/2009/10/my-top-10-personal-favorite-and-overlooked-posts/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 05:00:06 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[About The Blog]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1753</guid>
		<description><![CDATA[I apologize for just another link dumping of my own posts yet again, but i have some posts that are personal favorites of mine but that didn&#8217;t quite get the attention that i thought they deserved.  Instead of thinking that they suck (which they very well might) i tend to tell myself that these [...]]]></description>
			<content:encoded><![CDATA[<p>I apologize for just another link dumping of my own posts yet again, but i have some posts that are personal favorites of mine but that didn&#8217;t quite get the attention that i thought they deserved.  Instead of thinking that they suck (which they very well might) i tend to tell myself that these unfortunate posts were merely overlooked <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<ol>
<li><a href="http://davybrion.com/blog/2009/01/the-life-and-times-of-a-bug/">The Life And Times Of A Bug</a>
<p>
This is definitely my nr 1 favorite post.  I guess it was a bit too far out there for most people but you have to admit at least one thing: you&#8217;ve never read anything like that before :p
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/01/ethics-in-software-development-pragmatism-over-dogmatism/">Ethics In Software Development: Pragmatism Over Dogmatism</a>
<p>
Dogmatism of any kind is wrong.  Pragmatism isn&#8217;t always ideal either, but a healthy balance between them is a necessity in this business.
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/02/buzzword-driven-development-isnt-gonna-help-you/">Buzzword Driven Development Isn’t Gonna Help You</a>
<p>
I hate it when people go overboard with buzzwords, and it shows in this post.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/09/make-it-work-make-it-clean/">Make It Work, Make It Clean?</a>
<p>
I really loved this post&#8230; it&#8217;s about an approach that one of my friends used (and still uses from time to time).  I certainly don&#8217;t recommend this approach to anyone because i can only imagine a handful of people pulling this off successfully, but it&#8217;s interesting nonetheless.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/08/test-doubles-when-to-not-use-them/">Test Doubles: When To (Not) Use Them</a>
<p>
I actually think this is pretty good advice about when to use test doubles, and when not to.  Unfortunately, hardly anyone read it <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/01/which-path-to-follow/">Which Path To Follow?</a>
<p>
Which path should one follow if one is merely looking to improve continuously?
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/05/thoughts-on-code-reviews/">Thoughts on code reviews</a>
<p>
Discusses a different approach i once took for a code review.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/07/is-sql-going-the-way-of-disco/">Is SQL going the way of Disco?</a>
<p>
People are writing less and less SQL&#8230; my question was: why should they?
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/06/are-women-better-developers-than-men/">Are women better developers than men?</a>
<p>
If the majority of male developers sucks, is it a stretch to think that maybe women on average would be better at it than we are?
</p>
</li>
<li><a href="http://davybrion.com/blog/2007/06/programming-languages-dont-matter/">Programming Languages Don’t Matter</a>
<p>
They really, really don&#8217;t.
</p>
</li>
</ol>
<p>Hopefully some of you will like a few of these posts now <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/bBDFNGGzk3N-g4RDkpAQKebyjHI/0/da"><img src="http://feedads.g.doubleclick.net/~a/bBDFNGGzk3N-g4RDkpAQKebyjHI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/bBDFNGGzk3N-g4RDkpAQKebyjHI/1/da"><img src="http://feedads.g.doubleclick.net/~a/bBDFNGGzk3N-g4RDkpAQKebyjHI/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=v5yCDAWRbvw:9t2MIWOXjss:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=v5yCDAWRbvw:9t2MIWOXjss:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=v5yCDAWRbvw:9t2MIWOXjss:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/v5yCDAWRbvw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/my-top-10-personal-favorite-and-overlooked-posts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/my-top-10-personal-favorite-and-overlooked-posts/</feedburner:origLink></item>
		<item>
		<title>Stop Exposing Collections Already!</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/0BJYI5t369w/</link>
		<comments>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 05:00:09 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Code Quality]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1724</guid>
		<description><![CDATA[Every time i see code with properties that return List&#60;T&#62;, IList&#60;T&#62;, ICollection&#60;T&#62; or any other of the types which allow you to modify the contents of a collection, i cringe a little.  In most cases, the only reason to expose the contents of the collection is for read-only usage.  You want people to [...]]]></description>
			<content:encoded><![CDATA[<p>Every time i see code with properties that return List&lt;T&gt;, IList&lt;T&gt;, ICollection&lt;T&gt; or any other of the types which allow you to modify the contents of a collection, i cringe a little.  In most cases, the only reason to expose the contents of the collection is for read-only usage.  You want people to be able to use the objects in the collection, but why on earth do so many developers continuously enable someone else to modify the internal state of the collection that they are holding? </p>
<p>The problem that i have with exposing a collection&#8217;s values through a type which enables other pieces of code to modify the state of that collection is that it is effectively a pretty big breach of encapsulation.  You no longer have sole control over the contents of the collection.  Anyone can effectively add and remove elements from the collection, or even clear them entirely, without your object knowing about it.  Obviously, this can cause subtle side-effects in the behavior of your object.  Which can (and sooner or later will) lead to some quality time between you and your debugger.  You also no longer have the ability to easily change the type of collection you&#8217;re using which might prevent certain future improvements that you can make to a class.  And in case you&#8217;re wondering why you&#8217;d want to change the type of collection, check out <a href="http://davybrion.com/blog/2009/04/transparent-query-batching-through-your-repository/">an example where the benefit was huge</a>.</p>
<p>In a lot of cases, people do this because they don&#8217;t know any better or because it&#8217;s the easiest thing to do.  I can&#8217;t tell you how many times i&#8217;ve seen people expose List&lt;T&gt; simply so that some calling code could use the ForEach method of that list.  How long does it take to write a ForEach extension method for IEnumerable&lt;T&gt;?  Granted, it should&#8217;ve been included in the .NET framework already but just because it&#8217;s not doesn&#8217;t mean you can&#8217;t do so.</p>
<p>Here&#8217;s my philosophy on exposing collections: if i&#8217;m writing a class that is responsible for holding a collection, i take away every ability to directly modify the contents of the collection.  I typically expose the collection through an IEnumerable&lt;T&gt; property, and if necessary i will provide a specific AddXxx and RemoveXxx method.  That&#8217;s it.  You can do every single thing you need to do with the collection through IEnumerable&lt;T&gt;&#8217;s extension methods and through the AddXxx and RemoveXxx methods that i provide.  Meanwhile, i have every ability to choose the collection type that i want to use, or to modify it later on.  Furthermore, i can eliminate the possibility of side-effects in my code due to unexpected externally caused mutations in the state of the collection.</p>
<p>The only situation in which i think it&#8217;s OK to return a real collection type is if you have a method which is responsible for creating a list, but is not supposed to hold on to it.  In that case, the created collection is clearly intended to be used in whatever way makes sense by the consumer.  After all, you created the collection specifically for the consumer so they can do whatever they want with it.  But if you need to hold a reference to the collection for some reason, then you probably also have more responsibilities than merely creating the collection.  In that case, the collection is yours and you should encapsulate it as such.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/8dr6EM6GUV_Y_1hrq4nAKzmWoJg/0/da"><img src="http://feedads.g.doubleclick.net/~a/8dr6EM6GUV_Y_1hrq4nAKzmWoJg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/8dr6EM6GUV_Y_1hrq4nAKzmWoJg/1/da"><img src="http://feedads.g.doubleclick.net/~a/8dr6EM6GUV_Y_1hrq4nAKzmWoJg/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=0BJYI5t369w:IiCqQjwHw8M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=0BJYI5t369w:IiCqQjwHw8M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=0BJYI5t369w:IiCqQjwHw8M:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/0BJYI5t369w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/stop-exposing-collections-already/</feedburner:origLink></item>
		<item>
		<title>Prefixing Instance Fields?</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/invBVG7Gln0/</link>
		<comments>http://davybrion.com/blog/2009/10/prefixing-instance-fields/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 05:00:20 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Code Quality]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1778</guid>
		<description><![CDATA[I was talking to a friend the other day about prefixing instance fields.  His opinion was that by prefixing instance field names (with an underscore for example) you have the benefit of immediately showing the difference in scope between the usages of a local variable in a method, or the usage of an instance [...]]]></description>
			<content:encoded><![CDATA[<p>I was talking to a friend the other day about prefixing instance fields.  His opinion was that by prefixing instance field names (with an underscore for example) you have the benefit of immediately showing the difference in scope between the usages of a local variable in a method, or the usage of an instance field.  </p>
<p>There is indeed a legitimate possible problem here: any possible mutation of an instance field in a method can have an intended or unintended impact somewhere else in the class since the mutation will outlive the duration of the method call.  So it is important to be aware of this and that you are entirely sure that any possible mutation is actually intended and that no unintended mutations will occur because of some faulty code in a method.  I don&#8217;t however think that prefixing field names is the best solution to avoid this problem.  There are two practices that i recommend to easily avoid unintended mutations of fields in methods.  </p>
<p>The first is to use as few fields as possible.  To avoid any misunderstandings: i&#8217;m not saying that you shouldn&#8217;t use fields!  I&#8217;m just saying that fields need to justify their existence.  Fields inherently make up the state of an object, so if you don&#8217;t need a particular piece of data to hang around longer than say, the duration of a method call, then don&#8217;t put that data in a field.  If you need to access that data in another method that will be called from the originally called method you can simply pass it to the other method with a method parameter.  If you notice that you&#8217;re frequently passing the same pieces of data to a couple of methods, then you probably need to <a href="http://www.refactoring.com/catalog/introduceParameterObject.html">introduce a parameter object</a>.  Simply put: do not use fields unless the data you want to store in them are an inherent part of the object and the data needs to hang around <strong>for a while</strong>.</p>
<p>The second practice is too keep your methods short and focused.  If you&#8217;re working with large methods, then i can certainly understand why you would want all the help you can get to easily spot mutations of fields.  There&#8217;s only one question: why are you working with large methods?  There are truly very few legitimate reasons for not splitting up large methods into separate reusable methods.  When you use many small methods instead of large methods, <strong>it&#8217;s much easier to keep your focus on what any particular piece of code is supposed to do, and what it&#8217;s really doing</strong>.  And if your methods are short, you will very easily see when a field is being used instead of a local variable.</p>
<p>The combination of these two approaches leads to a lot less unintended mutations of fields.  And as a bonus, you get a lot more readable and maintainable code as well.  If however you still like to get some visual help in identifying field usage in methods, you can enable the Color Identifiers feature in Resharper (available in the Options window under Code Inspection &#8211; Settings &#8211; Color identifiers) which will result in fields being represented with a different color from regular variables. </p>
<p>What do you think about this? Do you still prefer to prefix field names, and if so, why?</p>

<p><a href="http://feedads.g.doubleclick.net/~a/T4Bx2bRzkq1oeLOb48Hubsb7dps/0/da"><img src="http://feedads.g.doubleclick.net/~a/T4Bx2bRzkq1oeLOb48Hubsb7dps/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/T4Bx2bRzkq1oeLOb48Hubsb7dps/1/da"><img src="http://feedads.g.doubleclick.net/~a/T4Bx2bRzkq1oeLOb48Hubsb7dps/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=invBVG7Gln0:6Shm2RVZs1E:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=invBVG7Gln0:6Shm2RVZs1E:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=invBVG7Gln0:6Shm2RVZs1E:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/invBVG7Gln0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/prefixing-instance-fields/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/prefixing-instance-fields/</feedburner:origLink></item>
		<item>
		<title>Finally Some Real Improvements For Automated Testing In .NET</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/HlkpgTN_BsY/</link>
		<comments>http://davybrion.com/blog/2009/10/finally-some-real-improvements-for-automated-testing-in-net/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 13:27:51 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1787</guid>
		<description><![CDATA[As most of you already know, i&#8217;m a proponent of unit testing and automated tests in general.  There is however one inherent flaw that comes with the unit testing approach: if you didn&#8217;t think of a problem, you didn&#8217;t write a test for it, and thus, you&#8217;re not protected from that problem.  
In [...]]]></description>
			<content:encoded><![CDATA[<p>As most of you already know, i&#8217;m a proponent of unit testing and automated tests in general.  There is however one inherent flaw that comes with the unit testing approach: if you didn&#8217;t think of a problem, you didn&#8217;t write a test for it, and thus, you&#8217;re not protected from that problem.  </p>
<p>In the past two years we&#8217;ve seen the .NET world gradually accept the concept of TDD, and it has been extended somewhat with the whole BDD thing.  However, there hasn&#8217;t been a single fundamental change which allows you to take away that inherent flaw.  Every single variation that has been introduced in the TDD/BDD world is honestly very minor (dare i even say pointless?) because it all boils down to different syntax and slightly different ways of organizing your test fixtures.  But again, even if you are doing the very latest flavor of the month when it comes to automated tests, you&#8217;re still not protected from problems that you didn&#8217;t think of.</p>
<p><a href="http://kilfour.wordpress.com/">Mark Meyers</a>, a former coworker of mine, has been working hard on porting <a href="http://www.cs.chalmers.se/~rjmh/QuickCheck/">QuickCheck</a> to .NET and the result is the <a href="http://code.google.com/p/quicknet/">QuickNet</a> project.  Instead of being yet another minor evolutionary step in the automated testing world for .NET, you could actually consider this to be a revolutionary improvement.  For the .NET world that is, since other developer communities have adopted this new approach already some time ago.</p>
<p>I highly encourage you to check out what QuickNet is about.  I&#8217;m about to get much more involved with the project, from a development point of view as well as using it and trying to spread the good word.  This is something that actually can offer you substantial benefits over classical unit testing approaches so you really would be doing yourself a huge favor in giving this a shot.  Keep in mind that it&#8217;s still pretty early and that the codebase is still going through some major changes.  There is no official release yet, so breaking changes can (and probably will) occur if you already try it out.</p>
<p>There also isn&#8217;t any official documentation yet either, but Mark has some <a href="http://kilfour.wordpress.com/category/quicknet/">great posts</a> about QuickNet&#8217;s usage and there&#8217;s also a couple of nice examples in the code already.  In fact, we also have a few QuickNet tests to test&#8230; well&#8230; QuickNet itself.  Head on over to some or all of the links i put in this post, and start learning <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/KRiB_DQUDJi8N76Ax1XQR7jv40s/0/da"><img src="http://feedads.g.doubleclick.net/~a/KRiB_DQUDJi8N76Ax1XQR7jv40s/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/KRiB_DQUDJi8N76Ax1XQR7jv40s/1/da"><img src="http://feedads.g.doubleclick.net/~a/KRiB_DQUDJi8N76Ax1XQR7jv40s/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=HlkpgTN_BsY:-iRAlUalDHc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=HlkpgTN_BsY:-iRAlUalDHc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=HlkpgTN_BsY:-iRAlUalDHc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/HlkpgTN_BsY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/finally-some-real-improvements-for-automated-testing-in-net/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/finally-some-real-improvements-for-automated-testing-in-net/</feedburner:origLink></item>
		<item>
		<title>Testability Of Date-Dependent Code</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/IcCEuWFn7ao/</link>
		<comments>http://davybrion.com/blog/2009/10/testability-of-date-dependent-code/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 05:00:30 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1711</guid>
		<description><![CDATA[Just read a post by Jonathan Oliver where he talks about a solution he saw to tackle the problem of date-dependent code in tests.  As you probably know, code that uses the current date can very easily cause testability problems.  Code that accesses DateTime.Now or DateTime.UctNow can quite easily cause tests to fail [...]]]></description>
			<content:encoded><![CDATA[<p>Just read a <a href="http://jonathan-oliver.blogspot.com/2009/10/ddd-entity-injection-and-mocking-time.html">post</a> by Jonathan Oliver where he talks about a solution he saw to tackle the problem of date-dependent code in tests.  As you probably know, code that uses the current date can very easily cause testability problems.  Code that accesses DateTime.Now or DateTime.UctNow can quite easily cause tests to fail for no valid reason when the tests happen to run on a certain date, on weekend days, at the end of the month, etc&#8230; </p>
<p>The problem obviously is that you can&#8217;t override the value that DateTime.Now will return during your tests.  Many people seem to resort to using some kind of DateTimeService dependency which each piece of code that needs the current date will use.  Basically, something like this:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IDateTimeService</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">DateTime</span> Now { <span class="cb1">get</span>; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>The implementation that will be used at runtime then looks like this:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">DateTimeService</span> : <span class="cb2">IDateTimeService</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb2">DateTime</span> Now</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">get</span> { <span class="cb1">return</span> <span class="cb2">DateTime</span>.Now; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Code that needs the current date simply declares a dependency on IDateTimeService and at run-time the IOC container will inject an instance of DateTimeService to fulfill the IDateTimeService dependency.  At test-time, the IDateTimeService is mocked so you can easily set which date should be returned for each test. </p>
<p>Personally, i&#8217;m not a fan of this approach.  I mean, i use the same approach for most dependencies but for simply getting the current date this is a bit too much IMO.</p>
<p>Instead, we simply use something like this:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb1">class</span> <span class="cb2">DateTimeProvider</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb2">DateTime</span>? dateTimeToReturn;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb2">DateTime</span> Now</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">get</span> { <span class="cb1">return</span> dateTimeToReturn == <span class="cb1">null</span> ? <span class="cb2">DateTime</span>.Now : dateTimeToReturn.Value; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb1">void</span> SetDateTimeToReturn(<span class="cb2">DateTime</span> overriddenCurrentDateTime)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dateTimeToReturn = overriddenCurrentDateTime;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb1">void</span> ResetCurrentDateTime()</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dateTimeToReturn = <span class="cb1">null</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>And our real code simply calls DateTimeProvider.Now instead of DateTime.Now.  In our tests, we call the SetDateTimeToReturn method to provide the date that we want to use for a particular test.  At the end of the test, simply call the ResetCurrentDateTime method and that&#8217;s it.</p>
<p>I generally don&#8217;t like static methods but in this case, this is a very simple solution to this specific problem.  And you know how the saying goes: &#8220;do the simplest thing that could possibly work&#8221;.  And as long as that works, resist the urge to change it.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/RWW-LKGq9nx-fpoCK-beY5zF2nI/0/da"><img src="http://feedads.g.doubleclick.net/~a/RWW-LKGq9nx-fpoCK-beY5zF2nI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/RWW-LKGq9nx-fpoCK-beY5zF2nI/1/da"><img src="http://feedads.g.doubleclick.net/~a/RWW-LKGq9nx-fpoCK-beY5zF2nI/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=IcCEuWFn7ao:cPr4mrbiri8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=IcCEuWFn7ao:cPr4mrbiri8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=IcCEuWFn7ao:cPr4mrbiri8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/IcCEuWFn7ao" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/testability-of-date-dependent-code/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/testability-of-date-dependent-code/</feedburner:origLink></item>
		<item>
		<title>Database Audit Trails: How Would You Do It?</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/Q2UNMknV6j8/</link>
		<comments>http://davybrion.com/blog/2009/10/database-audit-trails-how-would-you-do-it/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 17:40:03 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1764</guid>
		<description><![CDATA[For those of you wondering what the background story on the previous post is about: we had a meeting today to discuss 2 alternatives on how we should implement the database audit trail for a new project we&#8217;re doing.  These are the functional requirements:

We have to be able to easily retrieve what user X [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you wondering what the background story on the <a href="http://davybrion.com/blog/2009/10/wrong-on-so-many-levels/">previous post</a> is about: we had a meeting today to discuss 2 alternatives on how we should implement the database audit trail for a new project we&#8217;re doing.  These are the functional requirements:</p>
<ul>
<li>We have to be able to easily retrieve what user X changed in the database on day Y or during a certain time frame.</li>
<li>We need to know what record X looked like at point Y in time, and be able to show a historic list of changes for a specific record.</li>
<li>We need to configure the duration for which auditing data must be kept in the database on a per-table (or per-schema) level</li>
</ul>
<p>We&#8217;ve done this in the past, but never in a manner that is just perfect (or close enough to being perfect anyway) for both requirements.  So we discussed two alternative approaches to tackling this problem.</p>
<p>The first approach is to have 3 tables.  The first two tables would contain metadata about the database structure (tables and columns basically).  The third table would have a reference to each column with a previous value, the new value, who changed it and a timestamp of when the change occurred.  We would use database triggers that would store the appropriate auditing data in the third table for each action (insert/update/delete).  The benefits of this approach would be that it would be pretty easy to set up and also easy to query which changes were made by a certain user.   It would be a bit more complex to reconstruct the state of a record at a certain point in time, but it would still be doable.  The biggest downside (in my opinion), is that for each insert/update you would have X amount of inserts into the auditing table, and you&#8217;d also have to either perform some queries to get the identifier values of the relevant tables/columns or hardcode those identifier values in the generated triggers to avoid the cost of the extra select statements.  So if you have an entity with 12 columns, an insert into the entity&#8217;s table would mean 12 inserts into the auditing table.  Updating 4 columns of that entity would lead to 4 insert statements in the auditing table, etc&#8230;  </p>
<p>The other alternative that we considered was to use a specific auditing table for each real table, and every time a user does an insert/update/delete, insert one record into the table&#8217;s specific auditing table with the current state of the record along with a timestamp, the userid, and an indication of which action was performed (insert/update/delete).  This insert would also be done with database triggers.  The benefits of this approach would be that it would be very easy to show what the state of a certain entity was at a certain point in time, and that you&#8217;d only pay the cost of the single extra insert.  There are however two big downsides to this approach.  The first is that it&#8217;ll lead to an explosion of tables.  We&#8217;d probably store the auditing tables in a separate database but you still basically have twice the amount of tables (and scripts!) that you&#8217;d have to worry about compared to what you&#8217;d usually have.  We can very realistically expect a couple hundred of real tables in a few years, so you&#8217;d have to multiply that by two if you&#8217;d go for this auditing approach.  Another big downside is that it would be very expensive to perform a query which would tell you what user X changed at point Y in time (or during a time frame).</p>
<p>Neither solution appears to be ideal.  Also, this database will definitely grow a lot (more tables and obviously data) as we add more functionality and it&#8217;s pretty safe to consider this entire project as something that will be expanding in functionality for a couple of years to come.</p>
<p>So my question to you is: Which approach do you think is best, and why?  Or better yet, is there a better approach that we simply haven&#8217;t thought of yet?  I would definitely appreciate some feedback on this (either on the validity of both approaches or alternative approaches) so please do feel free to leave your thoughts in the comments <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/r0_bJFdG4o7Uve0fP6H7AeSAQhc/0/da"><img src="http://feedads.g.doubleclick.net/~a/r0_bJFdG4o7Uve0fP6H7AeSAQhc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/r0_bJFdG4o7Uve0fP6H7AeSAQhc/1/da"><img src="http://feedads.g.doubleclick.net/~a/r0_bJFdG4o7Uve0fP6H7AeSAQhc/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=Q2UNMknV6j8:5gpIrpOZaCk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=Q2UNMknV6j8:5gpIrpOZaCk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=Q2UNMknV6j8:5gpIrpOZaCk:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/Q2UNMknV6j8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/database-audit-trails-how-would-you-do-it/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/database-audit-trails-how-would-you-do-it/</feedburner:origLink></item>
		<item>
		<title>Wrong On So Many Levels</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/arMcK1nCCQU/</link>
		<comments>http://davybrion.com/blog/2009/10/wrong-on-so-many-levels/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 15:21:29 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Off Topic]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1760</guid>
		<description><![CDATA[Today i actually said the following sentence in a meeting:
&#8220;are you sure you can cleanly add the hardcoded identifier values in the generated database triggers?&#8221;
*shrug* What could possibly go wrong?
]]></description>
			<content:encoded><![CDATA[<p>Today i actually said the following sentence in a meeting:</p>
<p>&#8220;are you sure you can cleanly add the hardcoded identifier values in the generated database triggers?&#8221;</p>
<p>*shrug* What could possibly go wrong?</p>

<p><a href="http://feedads.g.doubleclick.net/~a/UvvQXsKtGqTYS2oh6x5VqvcldSo/0/da"><img src="http://feedads.g.doubleclick.net/~a/UvvQXsKtGqTYS2oh6x5VqvcldSo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/UvvQXsKtGqTYS2oh6x5VqvcldSo/1/da"><img src="http://feedads.g.doubleclick.net/~a/UvvQXsKtGqTYS2oh6x5VqvcldSo/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=arMcK1nCCQU:Firf0zqzWis:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=arMcK1nCCQU:Firf0zqzWis:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=arMcK1nCCQU:Firf0zqzWis:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/arMcK1nCCQU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/wrong-on-so-many-levels/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/wrong-on-so-many-levels/</feedburner:origLink></item>
		<item>
		<title>My Top 20 Most Popular Posts (So Far)</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/RWx7NQcWygg/</link>
		<comments>http://davybrion.com/blog/2009/10/my-top-20-most-popular-posts-so-far/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 09:41:31 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[About The Blog]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1746</guid>
		<description><![CDATA[I was looking at the blog&#8217;s all-time stats and figured it might be interesting to post a listing of the 20 most popular posts so far.  Unfortunately, there are only 5 or so of my personal favorite posts included  

Career Advice For Young Developers

This is one of my personal favorites so it was [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking at the blog&#8217;s all-time stats and figured it might be interesting to post a listing of the 20 most popular posts so far.  Unfortunately, there are only 5 or so of my personal favorite posts included <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<ol>
<li><a href="http://davybrion.com/blog/2008/10/career-advice-for-young-developers/">Career Advice For Young Developers</a>
<p>
This is one of my personal favorites so it was nice to see this one at the top of the list.  I really think the first 2 or 3 years of a developer&#8217;s career are crucial to the rest of it, so i hope this helped a few people.
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/04/at-this-point-id-prefer-java-developers-over-net-developers/">At This Point, I’d Prefer Java Developers Over .NET Developers</a>
<p>
This one caused a lot of controversy.  I&#8217;ve interviewed way too many lousy .NET developers in the past couple of years, so naturally i was very impressed when i had much more positive experiences when interviewing a couple of java developers.
</p>
</li>
<li><a href="http://davybrion.com/blog/2007/07/nhibernate-mapping-examples/">NHibernate Mapping Examples</a>
<p>
One of my very first posts.  It&#8217;s sorta outdated because it still uses NHibernate 1.2 and certainly doesn&#8217;t cover everything.  But i do think it&#8217;s still a pretty good reference for people who are getting started with NHibernate.
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/08/build-your-own-data-access-layer-series/">Build Your Own Data Access Layer Series</a>
<p>
This series was very successful and was a lot of fun for me to write as well.  Some of the individual posts got better stats than some of the other posts in this list, but since they&#8217;re all part of the same series i only included this one.  And every addition i&#8217;ll have to make to the data layer discussed in that series will be turned into a new post <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/03/upgrading-to-nhibernate-21/">Upgrading To NHibernate 2.1</a>
<p>
This is so typical&#8230; when we released NHibernate 2.1, we introduced a new required configuration setting.  If you don&#8217;t have the required configuration setting, NHibernate throws a very detailed exception telling you what to do to fix the problem.  Instead of reading the exception message and fixing the problem, most people just seem to copy/paste the exception message into their favorite search engine, click on the first link they see and then read what the blog post says instead of just reading the exception message.  Unbelievable.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/06/data-access-with-nhibernate/">Data Access With NHibernate</a>
<p>
Just my implementation of a Repository base class to be used with NHibernate.  I guess it got a lot of views just because it showed most of the basic things everybody will be doing with NHibernate, and should be doing with any other data layer for that matter.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/01/sending-nhibernate-entities-over-the-wcf-wire/">Sending NHibernate entities over the WCF wire</a>
<p>
This is a post that i definitely regret writing.  I am now totally against the concept of distributing entities, but when i wrote that post i didn&#8217;t think it was that horrible so i documented how to get it working.  Luckily, i never actually used this in a real production system.
</p>
</li>
<li><a href="http://davybrion.com/blog/recommended-books/">Recommended Books</a>
<p>
This is a page on my site instead of a post&#8230; but it&#8217;s gotten so many views that i guess it deserves to be in this list.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/06/managing-your-nhibernate-sessions/">Managing your NHibernate Sessions</a>
<p>
My Unit Of Work implementation which sort of abstracts the NHibernate session, but still makes it fully available in Repository classes.  Still using this quite happily in all of our applications <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/12/tired-of-working-with-big-visual-studio-solutions/">Tired Of Working With Big Visual Studio Solutions?</a>
<p>
We all know what a pain Visual Studio is with large solutions, right?  This trick at least reduces the pain.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/11/genesis-bridging-the-gap-between-requirement-and-code/">Genesis: Bridging The Gap Between Requirements And Code</a>
<p>
If you don&#8217;t know what this is about, then you definitely need to check it out!
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/">Must Everything Be Virtual With NHibernate?</a>
<p>
I recently revisited this topic with a detailed example, but this post is the theoretical explanation of why NHibernate wants you to use virtual properties and methods in your entity classes.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/07/the-request-response-service-layer/">The Request/Response Service Layer</a>
<p>
I got the idea from this from one of Ayende&#8217;s posts, so i can&#8217;t take credit for that.  But i am very proud of the implementation and it is still working extremely well for us.  I really can&#8217;t imagine going back to typical WCF services after having used this approach so successfully for over a year now.  I&#8217;m actually thinking about writing an extensive series of posts on this.  If you&#8217;d be interested in such a series, be sure to let me know&#8230; it might convince me to start writing it sooner rather than later <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/07/how-to-write-testable-aspnet-webforms/">How To Write Testable ASP.NET WebForms</a>
<p>
The MVP approach we use whenever we use WebForms.  Which is&#8230; well, hardly ever anymore (except for one never-ending project).  Silverlight FTW!
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/04/educate-developers-instead-of-protecting-them/">Educate Developers Instead Of Protecting Them</a>
<p>
My rant against the typical &#8220;we need to protect developers from theirselves&#8221;-approach that i see too often in this industry.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/12/what-it-takes-to-be-a-great-technical-lead/">What It Takes To Be A Great Technical Lead</a>
<p>
Once again, i don&#8217;t claim to live up to that i wrote in that post but i do think those are the qualities that a great technical lead has to have.
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/04/the-good-the-bad-and-the-ugly-in-the-net-world/">The Good, The Bad And The Ugly In The .NET World</a>
<p>
Some thoughts on why i think the .NET world isn&#8217;t as good as it could be.
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/11/why-on-earth-would-a-developer-do-this/">Why On Earth Would A Developer Do This?</a>
<p>
My reaction to reading a piece of example code in a book from one of the guru&#8217;s we all love and respect.
</p>
</li>
<li><a href="http://davybrion.com/blog/2009/10/slutty-types/">Slutty Types</a>
<p>
This one made it up here pretty fast.  I can only hope the name sticks and that i have at least introduced ONE thing to the world of software development <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />
</p>
</li>
<li><a href="http://davybrion.com/blog/2008/11/populating-entities-from-stored-procedures-with-nhibernate/">Populating Entities From Stored Procedures With NHibernate</a>
<p>
Sometimes, it actually makes sense to use a stored procedure here or there&#8230; so you might as well know how to get that output into an entity instance easily.
</p>
</li>
</ol>

<p><a href="http://feedads.g.doubleclick.net/~a/BNHGdjVPTiyk1EGdTkBd-Y7J6d8/0/da"><img src="http://feedads.g.doubleclick.net/~a/BNHGdjVPTiyk1EGdTkBd-Y7J6d8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/BNHGdjVPTiyk1EGdTkBd-Y7J6d8/1/da"><img src="http://feedads.g.doubleclick.net/~a/BNHGdjVPTiyk1EGdTkBd-Y7J6d8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=RWx7NQcWygg:IMmxcRXs4oo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=RWx7NQcWygg:IMmxcRXs4oo:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=RWx7NQcWygg:IMmxcRXs4oo:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/RWx7NQcWygg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/my-top-20-most-popular-posts-so-far/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/my-top-20-most-popular-posts-so-far/</feedburner:origLink></item>
		<item>
		<title>There’s Only One Thing You Can Learn From Code Coverage</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/iQjM0OVaXBo/</link>
		<comments>http://davybrion.com/blog/2009/10/theres-only-one-thing-you-can-learn-from-code-coverage/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 06:11:15 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1718</guid>
		<description><![CDATA[When you hear people talk about automated testing, the subject of code coverage typically comes up as well.  Code coverage is a metric which indicates how much of your production code is executed while your automated tests are running.  A lot of people think that high code coverage is a good thing, because [...]]]></description>
			<content:encoded><![CDATA[<p>When you hear people talk about automated testing, the subject of code coverage typically comes up as well.  Code coverage is a metric which indicates how much of your production code is <strong>executed</strong> while your automated tests are running.  A lot of people think that high code coverage is a good thing, because it means that a high percentage of their production code is being executed by their tests.  That&#8217;s gotta be a good thing, right?</p>
<p>Well, not necessarily.  You can very easily write bad tests which execute most, or even all of your production code, while the tests themselves probably don&#8217;t cover everything that really needs to be covered.  Even if you&#8217;re a disciplined developer who is very careful not to make mistakes, the odds that your code will contain bugs that your tests didn&#8217;t catch are still relatively likely.  For those of you who write automated tests (either test-first or test-after): how often has it happened to you that a bug was discovered later on for some edge case that you didn&#8217;t think of when writing the production code or the test code?  No matter how good you are, this has happened to you on more than one occasion.  And it will happen again in the future.  </p>
<p>Here&#8217;s the important question though: how frequently does it happen that bugs are discovered in pieces of code which have high coverage during the test-runs?  It might not happen every day, it might not happen every week but it definitely happens occasionally and in some cases even frequently.  What exactly does that tell you about the value of a metric such as code coverage?  You might think that if this situation comes up, you don&#8217;t have proper tests in place but that&#8217;s not necessarily the case.  You can have an incredibly good test fixture (or several) for a certain piece of code but if you and/or your teammates didn&#8217;t happen to think of a certain edge-case or even something that might be obvious, then you didn&#8217;t write tests for that.  Even though the build report might indicate 100% coverage for that piece of code.</p>
<p>I&#8217;ve always thought that <strong>code coverage is only meaningful when it&#8217;s low</strong>.  Low code coverage can at least definitively tell you that certain parts of code are never executed during test-runs and are thus completely untested. However, it will never definitively tell you which parts of code (or how much of it) are guaranteed to be covered <strong>correctly</strong> by your tests.  As such, i would say that code coverage can only be used as an indication that something is wrong, but never as an indication that something is good/right/correct/great.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/WhNR7j_VS0VaTthAtCEuQkKR7GA/0/da"><img src="http://feedads.g.doubleclick.net/~a/WhNR7j_VS0VaTthAtCEuQkKR7GA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/WhNR7j_VS0VaTthAtCEuQkKR7GA/1/da"><img src="http://feedads.g.doubleclick.net/~a/WhNR7j_VS0VaTthAtCEuQkKR7GA/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=iQjM0OVaXBo:UB88ZSmaDKc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=iQjM0OVaXBo:UB88ZSmaDKc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=iQjM0OVaXBo:UB88ZSmaDKc:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/iQjM0OVaXBo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/theres-only-one-thing-you-can-learn-from-code-coverage/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/theres-only-one-thing-you-can-learn-from-code-coverage/</feedburner:origLink></item>
		<item>
		<title>Build Your Own Data Access Layer: Enabling Bulk Inserts</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/2uBO0hYxrjQ/</link>
		<comments>http://davybrion.com/blog/2009/10/build-your-own-data-access-layer-enabling-bulk-inserts/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 10:29:43 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Build Your Own DAL]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1733</guid>
		<description><![CDATA[Note: This post is part of a series. Be sure to read the introduction here.
I know i wrapped up the series already, but i just had to add the ability to do bulk inserts to this data layer so i figured i&#8217;d might as well post about it.  Ayende already talked about how to [...]]]></description>
			<content:encoded><![CDATA[<p>Note: This post is part of a series. Be sure to read the introduction <a href="http://davybrion.com/blog/2009/08/build-your-own-data-access-layer-series/">here</a>.</p>
<p>I know i wrapped up the series already, but i just had to add the ability to do bulk inserts to this data layer so i figured i&#8217;d might as well post about it.  Ayende already talked about how to enable the ability to batch inserts (or updates and deletes) <a href="http://ayende.com/Blog/archive/2006/09/13/OpeningUpQueryBatching.aspx">here</a> and <a href="http://ayende.com/Blog/archive/2006/09/14/ThereBeDragonsRhinoCommonsSqlCommandSet.aspx">here</a> so i&#8217;m going to skip that part.  I used the exact same trick and created a PublicSqlCommandSet class which wraps the hidden SqlCommandSet class.  Again, if you have no idea what i&#8217;m talking about in that last sentence then you need to read Ayende&#8217;s 2 posts that i just linked to <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>After that, adding the bulk insert feature to the DAL was as simple as creating this class:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BulkInsertAction</span> : <span style="color: #2b91af;">DatabaseAction</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> BulkInsertAction(<span style="color: #2b91af;">SqlConnection</span> connection, <span style="color: #2b91af;">SqlTransaction</span> transaction, <span style="color: #2b91af;">MetaDataStore</span> metaDataStore, </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">EntityHydrater</span> hydrater, <span style="color: #2b91af;">SessionLevelCache</span> sessionLevelCache) </p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; : <span style="color: blue;">base</span>(connection, transaction, metaDataStore, hydrater, sessionLevelCache) {}</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Insert&lt;TEntity&gt;(<span style="color: #2b91af;">IEnumerable</span>&lt;TEntity&gt; entities, <span style="color: blue;">int</span> batchSize, <span style="color: blue;">int</span> commandTimeOut)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> tableInfo = MetaDataStore.GetTableInfoFor&lt;TEntity&gt;();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> insertStatement = tableInfo.GetInsertStatementWithoutReturningTheIdentityValue();</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> sqlCommandSet = <span style="color: blue;">new</span> <span style="color: #2b91af;">PublicSqlCommandSet</span> { CommandTimeout = commandTimeOut, Connection = GetConnection(), Transaction = GetTransaction() };</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> entity <span style="color: blue;">in</span> entities)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> currentCommand = CreateCommand();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentCommand.CommandText = insertStatement;</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> parameterInfo <span style="color: blue;">in</span> tableInfo.GetParametersForInsert(entity))</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentCommand.CreateAndAddInputParameter(parameterInfo.DbType, <span style="color: #a31515;">&quot;@&quot;</span> + parameterInfo.Name, parameterInfo.Value);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sqlCommandSet.Append(currentCommand);</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (sqlCommandSet.CommandCount == batchSize)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ExecuteCurrentBatch(sqlCommandSet);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sqlCommandSet = <span style="color: blue;">new</span> <span style="color: #2b91af;">PublicSqlCommandSet</span> { CommandTimeout = commandTimeOut, Connection = GetConnection(), Transaction = GetTransaction() };</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (sqlCommandSet.CommandCount &gt; 0)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ExecuteCurrentBatch(sqlCommandSet);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">void</span> ExecuteCurrentBatch(<span style="color: #2b91af;">PublicSqlCommandSet</span> sqlCommandSet)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">try</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sqlCommandSet.ExecuteNonQuery();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">finally</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sqlCommandSet.Dispose();</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>And then adding this to the Session class:</p>
<p><code></p>
<div style="font-family: Consolas; font-size: 9pt; color: black; background: white;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> BulkInsert&lt;TEntity&gt;(<span style="color: #2b91af;">IEnumerable</span>&lt;TEntity&gt; entities)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CreateAction&lt;<span style="color: #2b91af;">BulkInsertAction</span>&gt;().Insert(entities, 50, 200);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>Obviously, the method signature was also added to the ISession interface.  The batch-size and commandtimeout parameters are currently hardcoded but they should come from some kind of configuration file.</p>
<p>All in all, pretty easy stuff <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/TCWbf9T8v3fH7Xalo71fhsKAiw8/0/da"><img src="http://feedads.g.doubleclick.net/~a/TCWbf9T8v3fH7Xalo71fhsKAiw8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/TCWbf9T8v3fH7Xalo71fhsKAiw8/1/da"><img src="http://feedads.g.doubleclick.net/~a/TCWbf9T8v3fH7Xalo71fhsKAiw8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=2uBO0hYxrjQ:BJfJMoV3pGQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=2uBO0hYxrjQ:BJfJMoV3pGQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=2uBO0hYxrjQ:BJfJMoV3pGQ:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/2uBO0hYxrjQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/build-your-own-data-access-layer-enabling-bulk-inserts/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/build-your-own-data-access-layer-enabling-bulk-inserts/</feedburner:origLink></item>
		<item>
		<title>Slutty Types</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/cJIeTmLldPQ/</link>
		<comments>http://davybrion.com/blog/2009/10/slutty-types/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 17:57:11 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1729</guid>
		<description><![CDATA[Slutty Types are types which:

give you access to their privates without too many difficulties
don&#8217;t really care about your intentions, or if they do, aren&#8217;t very clear on that
occasionally seem like a good short-term fix
can be used in a variety of ways, with different outcomes and none of them are guaranteed
can not to be trusted
really need [...]]]></description>
			<content:encoded><![CDATA[<p>Slutty Types are types which:</p>
<ul>
<li>give you access to their privates without too many difficulties</li>
<li>don&#8217;t really care about your intentions, or if they do, aren&#8217;t very clear on that</li>
<li>occasionally seem like a good short-term fix</li>
<li>can be used in a variety of ways, with different outcomes and none of them are guaranteed</li>
<li>can not to be trusted</li>
<li>really need to be tested</li>
<li>will burn you sooner or later if you&#8217;re not careful</li>
<li>become even more of a mess during the aging process</li>
</ul>
<p>Feel free to add to the list <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/airRftX0mDlS_HQb4OE2pdfZrBw/0/da"><img src="http://feedads.g.doubleclick.net/~a/airRftX0mDlS_HQb4OE2pdfZrBw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/airRftX0mDlS_HQb4OE2pdfZrBw/1/da"><img src="http://feedads.g.doubleclick.net/~a/airRftX0mDlS_HQb4OE2pdfZrBw/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=cJIeTmLldPQ:VsqWce3bM5M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=cJIeTmLldPQ:VsqWce3bM5M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=cJIeTmLldPQ:VsqWce3bM5M:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/cJIeTmLldPQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/slutty-types/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/slutty-types/</feedburner:origLink></item>
		<item>
		<title>Event Aggregation</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/YehXSZVo7nk/</link>
		<comments>http://davybrion.com/blog/2009/10/event-aggregation/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 14:12:52 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1708</guid>
		<description><![CDATA[We&#8217;re experimenting at work with a bit of a different approach as to how we structure our views and how they will interact with each other.  We already know that our views will be reused in many different contexts so having them communicate with other views is something that needs to be done in [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re experimenting at work with a bit of a different approach as to how we structure our views and how they will interact with each other.  We already know that our views will be reused in many different contexts so having them communicate with other views is something that needs to be done in a very loosely coupled manner.  I don&#8217;t want any of the views to even know about the existence of other views, let alone having them know about specific instances of them.</p>
<p>But these views do have to interact with each other.  I didn&#8217;t want to use typical events because that would require either a certain view to know about another view to be able to subscribe to its events, or you&#8217;d need some other component which knows which views need to be hooked to each other.  We really need maximum flexibility for what we have in mind with our views, so it only made sense to finally start using the <a href="http://martinfowler.com/eaaDev/EventAggregator.html">Event Aggregation pattern</a>.  The idea is that a view can basically publish events, without knowing who is subscribed to these events, and that suscribers will be notified whenever these events occur.  However, subscribers don&#8217;t know anything about who is publishing the events.  Instead, both publishers and subscribers only know of the Event Aggregator.  A publisher tells the aggregator to publish an event to all subscribed listeners for that event.  Each subscribers simply tells the aggregator &#8220;i&#8217;d like to be notified whenever this event occurs, and i don&#8217;t care where it comes from&#8221;.<br />
Plenty of implementations of this pattern can be found online already, so i figured: why not add my own? :p</p>
<p>Fist of all, an event is nothing more than a class that inherits from this class:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">abstract</span> <span class="cb1">class</span> <span class="cb2">Event</span> {}</p>
</div>
<p></code></p>
<p>Every event should inherit from this class, and add whatever necessary properties that are important for that particular type of event. </p>
<p>If a class is interested in listening to a specific event, it needs to implement the following interface:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IListener</span> { }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IListener</span>&lt;TEvent&gt; : <span class="cb2">IListener</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Handle(TEvent receivedEvent);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>If a class is interested in multiple events, it simply needs to implement the generic IListener interface for each type of event that it wants to handle.</p>
<p>Then we obviously need the Event Aggregator.  I wanted an aggregator that allowed listeners to either subscribe/unsubscribe to/from very specific events, or just subscribe/unsubscribe to/from whatever it supports.  So i have the following IEventAggregator interface:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IEventAggregator</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Publish&lt;TEvent&gt;(TEvent message) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Publish&lt;TEvent&gt;() <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>, <span class="cb1">new</span>();</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Subscribe(<span class="cb2">IListener</span> listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Unsubscribe(<span class="cb2">IListener</span> listener);</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Subscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">void</span> Unsubscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>The Subscribe and Unsubscribe methods that simply take an IListener reference will either subscribe or unsubscribe the given listener to/from every event that it can handle.  In other words, for every generic IListener interface that it implements.  Yet you also have the ability to subscribe/unsubscribe from a specific event type.</p>
<p>And here&#8217;s the implementation:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
.cb3 { color: #a31515; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">EventAggregator</span> : <span class="cb2">IEventAggregator</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">readonly</span> <span class="cb1">object</span> listenerLock = <span class="cb1">new</span> <span class="cb1">object</span>();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">protected</span> <span class="cb1">readonly</span> <span class="cb2">Dictionary</span>&lt;<span class="cb2">Type</span>, <span class="cb2">List</span>&lt;<span class="cb2">IListener</span>&gt;&gt; listeners = <span class="cb1">new</span> <span class="cb2">Dictionary</span>&lt;<span class="cb2">Type</span>, <span class="cb2">List</span>&lt;<span class="cb2">IListener</span>&gt;&gt;();</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">readonly</span> <span class="cb2">IDispatcher</span> dispatcher;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> EventAggregator(<span class="cb2">IDispatcher</span> dispatcher)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">this</span>.dispatcher = dispatcher;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Subscribe(<span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ForEachListenerInterfaceImplementedBy(listener, Subscribe);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Unsubscribe(<span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ForEachListenerInterfaceImplementedBy(listener, Unsubscribe);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb1">void</span> ForEachListenerInterfaceImplementedBy(<span class="cb2">IListener</span> listener, <span class="cb2">Action</span>&lt;<span class="cb2">Type</span>, <span class="cb2">IListener</span>&gt; action)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> listenerTypeName = <span class="cb1">typeof</span>(<span class="cb2">IListener</span>).Name;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> interfaceType <span class="cb1">in</span> listener.GetType().GetInterfaces().Where(i =&gt; i.Name.StartsWith(listenerTypeName)))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Type</span> typeOfEvent = GetEventType(interfaceType);</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (typeOfEvent != <span class="cb1">null</span>)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; action(typeOfEvent, listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb2">Type</span> GetEventType(<span class="cb2">Type</span> type)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (type.GetGenericArguments().Count() &gt; 0)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> type.GetGenericArguments()[0];</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">null</span>;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Subscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Subscribe(<span class="cb1">typeof</span>(TEvent), listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">protected</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Subscribe(<span class="cb2">Type</span> typeOfEvent, <span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">lock</span> (listenerLock)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (!listeners.ContainsKey(typeOfEvent))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listeners.Add(typeOfEvent, <span class="cb1">new</span> <span class="cb2">List</span>&lt;<span class="cb2">IListener</span>&gt;());</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (listeners[typeOfEvent].Contains(listener))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb2">InvalidOperationException</span>(<span class="cb3">&quot;You're not supposed to register to the same event twice&quot;</span>);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listeners[typeOfEvent].Add(listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Unsubscribe&lt;TEvent&gt;(<span class="cb2">IListener</span>&lt;TEvent&gt; listener) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Unsubscribe(<span class="cb1">typeof</span>(TEvent), listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">protected</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Unsubscribe(<span class="cb2">Type</span> typeOfEvent, <span class="cb2">IListener</span> listener)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">lock</span>(listenerLock)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (listeners.ContainsKey(typeOfEvent))</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; listeners[typeOfEvent].Remove(listener);</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Publish&lt;TEvent&gt;(TEvent message) <span class="cb1">where</span> TEvent : <span class="cb2">Event</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> typeOfEvent = <span class="cb1">typeof</span>(TEvent);</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">lock</span> (listenerLock)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (!listeners.ContainsKey(typeOfEvent)) <span class="cb1">return</span>;</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> listener <span class="cb1">in</span> listeners[typeOfEvent])</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> typedReference = (<span class="cb2">IListener</span>&lt;TEvent&gt;)listener;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dispatcher.BeginInvoke(() =&gt; typedReference.Handle(message));</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">virtual</span> <span class="cb1">void</span> Publish&lt;TEvent&gt;() <span class="cb1">where</span> TEvent : <span class="cb2">Event</span>, <span class="cb1">new</span>()</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Publish(<span class="cb1">new</span> TEvent());</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>In case you&#8217;re wondering&#8230; the IDispatcher interface is merely a way to wrap Silverlight&#8217;s real Dispatcher.  We wrap it so we can use a different implementation of the IDispatcher in our automated tests.  Other than that, the implementation is very straightforward.</p>
<p>We started using this very recently, so this implementation might change in the upcoming weeks, but for now it does what it needs to do and it does so pretty well.  In our case, every view&#8217;s Presenter automatically has an IEventAggregator property so whenever we need to publish an event, we can simply do something like EventAggregator.Publish(new SomeEvent(someParameter)).  Or, Presenters that need to listen to events can simply say EventAggregator.Subscribe(this) or only subscribe to some specific events whenever they need to and their specific Handle method will be called whenever someone publishes the event.  This also allowed us to get rid of the somewhat awkward syntax when testing events (subscription, unsubscription and the actual handing of events) with mocking frameworks.</p>
<p>And as a last bonus, i put a call to the Unsubscribe(IListener) method in the Dispose method of our base Presenter implementation.  Which means that none of them will be left subscribed to events by accident anymore <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feedads.g.doubleclick.net/~a/gQ99PvyZKjAJPa5SFytlkPJZw0I/0/da"><img src="http://feedads.g.doubleclick.net/~a/gQ99PvyZKjAJPa5SFytlkPJZw0I/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/gQ99PvyZKjAJPa5SFytlkPJZw0I/1/da"><img src="http://feedads.g.doubleclick.net/~a/gQ99PvyZKjAJPa5SFytlkPJZw0I/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=YehXSZVo7nk:SGLK-EWXaJ4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=YehXSZVo7nk:SGLK-EWXaJ4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=YehXSZVo7nk:SGLK-EWXaJ4:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/YehXSZVo7nk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/event-aggregation/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/event-aggregation/</feedburner:origLink></item>
		<item>
		<title>Thoughts On Intellectual Honesty And Personal Ambitions</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/kNpvTMJ0n0Y/</link>
		<comments>http://davybrion.com/blog/2009/10/thoughts-on-intellectual-honesty-and-personal-ambitions/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 17:38:40 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[Opinions]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1702</guid>
		<description><![CDATA[One of my favorite topics discussed in the classic Code Complete book (simply a must-read for every developer) is that of Intellectual Honesty.  It basically means being honest about what you know, what you don&#8217;t know, what you can do and what you can&#8217;t do.  It also means that you need to be [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite topics discussed in the classic <a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1254673947&#038;sr=8-1">Code Complete</a> book (simply a must-read for every developer) is that of Intellectual Honesty.  It basically means being honest about what you know, what you don&#8217;t know, what you can do and what you can&#8217;t do.  It also means that you need to be able to give credit where credit is due, and that you as a developer need to be able to accept solutions to problems no matter where they came from, even if it wasn&#8217;t your idea or if the idea/solution comes from someone you might not always agree with.</p>
<p>I&#8217;ve always thought that Intellectual Honesty is a very important trait that every developer needs to have.  I also believe that it is too frequently missing in this business.  Some people just want (or need) to be right all the time, even if they are wrong.  In software development, it&#8217;s generally in everyone&#8217;s best interest to go with the best possible solution to a given problem, while still keeping all relevant constraints in account.  Some developers have a genuine interest in the architecture that is used and how things should be done.  That is obviously a good thing and i wish there were more developers like that.  </p>
<p>I&#8217;m in a position where i have a lot of influence on architectural decisions and how we should develop our code.  But i&#8217;ve never wanted to be the only one who had to make these decisions, nor do i want people to just go ahead with every idea that i have.  I want people to get involved with these things.  I want feedback.  I want to hear ideas from other people that are better than my own.  It&#8217;s better for everyone if multiple people are involved in such discussions and decisions.  Get a couple of people together, discuss things, go over the options and eventually pick the best proposed solution no matter who came up with it.  That&#8217;s always been my approach and it always will be.  And i truly believe that it&#8217;s a healthy approach for everyone involved.</p>
<p>But there are always people who either want to be the only one who gets to decide these things, without input from others, or are generally unhappy in their job if they believe their ideas aren&#8217;t being considered.  </p>
<p>My question to these people is: why? Why does it matter if another person&#8217;s solution is chosen? Why would you need to be frustrated or disgruntled if your solution wasn&#8217;t chosen?  If you&#8217;re working for a company where your ideas aren&#8217;t even considered then sure, you have every right to be frustrated or disgruntled (and i should know, because i&#8217;ve worked in such a place in the past).  But if you&#8217;re working for a company where you know full well that everyone&#8217;s ideas are considered, then you really have no reason at all to complain if your solution wasn&#8217;t accepted.</p>
<p>Unless of course, you are more concerned about your personal ambitions instead of the actual results.  If all you care about is your status, position or whatever you want to call it, then you really are in the wrong business.  Sure, there are companies where you can make quite a career out of such an approach, but it&#8217;s just not healthy.  Not for you personally, nor for your employer, nor for your customers.  It doesn&#8217;t help anyone but yourself, and that sure as hell won&#8217;t last forever.  </p>
<p>Be honest, at least to yourself and preferably to the people you work with as well.  If your ideas don&#8217;t make it, then you might need to question your approach (and for some people specifically: your tactics), your real motivations, or perhaps even your skill level.  Again: be honest about what you can do, what you can&#8217;t do, what you know and what you don&#8217;t know.  And learn how to work with other people, instead of simply trying to get ahead of them.</p>
<p>I&#8217;d like to end this post with a quote from Dale Carnegie:</p>
<blockquote><p>Any fool can defend his or her mistakes, and most fools do.</p></blockquote>

<p><a href="http://feedads.g.doubleclick.net/~a/x04MiKRy-KYLaJnnkzTbHyfUuSQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/x04MiKRy-KYLaJnnkzTbHyfUuSQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/x04MiKRy-KYLaJnnkzTbHyfUuSQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/x04MiKRy-KYLaJnnkzTbHyfUuSQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=kNpvTMJ0n0Y:WPyUM4SZrGw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=kNpvTMJ0n0Y:WPyUM4SZrGw:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=kNpvTMJ0n0Y:WPyUM4SZrGw:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/kNpvTMJ0n0Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/thoughts-on-intellectual-honesty-and-personal-ambitions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/thoughts-on-intellectual-honesty-and-personal-ambitions/</feedburner:origLink></item>
		<item>
		<title>Why NHibernate Entities Need A Public Or Protected Parameterless Constructor</title>
		<link>http://feedproxy.google.com/~r/davybrion/~3/5stzfX9XzzY/</link>
		<comments>http://davybrion.com/blog/2009/10/why-nhibernate-entities-need-a-public-or-protected-parameterless-constructor/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 16:17:16 +0000</pubDate>
		<dc:creator>Davy Brion</dc:creator>
				<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://davybrion.com/blog/?p=1698</guid>
		<description><![CDATA[I have an older post where i discuss how you can implement a Value Object with NHibernate.  In that post i mentioned the following:
NHibernate allows a private default constructor for Value Objects, but for Entities you will need a default public or protected constructor as private is not sufficient.
I got the following comment from [...]]]></description>
			<content:encoded><![CDATA[<p>I have an older post where i discuss <a href="http://davybrion.com/blog/2009/03/implementing-a-value-object-with-nhibernate">how you can implement a Value Object with NHibernate</a>.  In that post i mentioned the following:</p>
<blockquote><p>NHibernate allows a private default constructor for Value Objects, but for Entities you will need a default public or protected constructor as private is not sufficient.</p></blockquote>
<p>I got the following comment from someone:</p>
<blockquote><p>
I too am trying to determine how well NHibernate lives up to the promise of persistence ignorance. I can definitely live with unnecessary private constructors, but I’m dubious about adding protected constructors just to support an ORM.</p>
<p>At any rate, I was surprised by the sentence I quoted, because I didn’t realize there were any circumstances in which NHibernate required protected default constructors.
</p></blockquote>
<p>Once again, the answer is related to the dynamic proxies that NHibernate uses.  Value Objects will never be proxied by NHibernate, so NHibernate only needs a private default constructor to create the instances.  If an entity is eligible for lazy loading however, then NHibernate will create a type which inherits from your entity (this is described in depth <a href="http://davybrion.com/blog/2009/03/must-everything-be-virtual-with-nhibernate/">here</a> and <a href="http://davybrion.com/blog/2009/09/must-everything-be-virtual-with-nhibernate-part-iii/">here</a>).  Which means that we really need either a public or protected constructor in entity classes that are eligible for lazy loading.  Consider the following class:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">SomeEntity</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> SomeEntity(<span class="cb1">string</span> someRequiredValue)</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">private</span> SomeEntity()</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>If we try to create the following derived class:</p>
<p><code></p>
<style type="text/css">
.cf { font-family: Consolas; font-size: 9pt; color: black; background: white; }
.cl { margin: 0px; }
.cb1 { color: blue; }
.cb2 { color: #2b91af; }
</style>
<div class="cf">
<p class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">SomeEntityProxy</span> : <span class="cb2">SomeEntity</span></p>
<p class="cl">&nbsp;&nbsp;&nbsp; {</p>
<p class="cl">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></code></p>
<p>We get the following compiler error:</p>
<p>Error	1	&#8216;ConsoleApplication1.SomeEntity.SomeEntity()&#8217; is inaccessible due to its protection level	</p>
<p>It&#8217;s a silly example, but it does show why entity types need at least a public or a protected default constructor and why a private one isn&#8217;t sufficient.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/U4Tsz0B9QHSxxEIsBGedhQP9_fM/0/da"><img src="http://feedads.g.doubleclick.net/~a/U4Tsz0B9QHSxxEIsBGedhQP9_fM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/U4Tsz0B9QHSxxEIsBGedhQP9_fM/1/da"><img src="http://feedads.g.doubleclick.net/~a/U4Tsz0B9QHSxxEIsBGedhQP9_fM/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/davybrion?a=5stzfX9XzzY:-X8DNQevTJ8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/davybrion?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/davybrion?a=5stzfX9XzzY:-X8DNQevTJ8:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/davybrion?i=5stzfX9XzzY:-X8DNQevTJ8:D7DqB2pKExk" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/davybrion/~4/5stzfX9XzzY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://davybrion.com/blog/2009/10/why-nhibernate-entities-need-a-public-or-protected-parameterless-constructor/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://davybrion.com/blog/2009/10/why-nhibernate-entities-need-a-public-or-protected-parameterless-constructor/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.859 seconds. --><!-- Cached page generated by WP-Super-Cache on 2009-11-08 21:49:25 -->
