<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David Laing's blog &#187; Architecture</title>
	<atom:link href="http://davidlaing.com/category/architecture/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidlaing.com</link>
	<description>Craftmanship over crap</description>
	<lastBuildDate>Mon, 19 Jul 2010 00:39:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-rare</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>DTOs: To Flatten Or Not To Flatten?</title>
		<link>http://davidlaing.com/2010/04/20/dtos-to-flatten-or-not-to-flatten/</link>
		<comments>http://davidlaing.com/2010/04/20/dtos-to-flatten-or-not-to-flatten/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 06:58:39 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Software Craftmanship]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[design_patterns]]></category>
		<category><![CDATA[dtos]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=200</guid>
		<description><![CDATA[that is the question&#8230;
I&#8217;m busy working on a JSON based API with client libraries in Flex, C# and Javascript (and eventually iPhone, JavaFX etc).
One of the things the API deals with is the concept of a Market.  Markets have a number of properties that &#8220;belong&#8221; to them, simplistically things like MarketId and Name.

[-]?View Code [...]]]></description>
			<content:encoded><![CDATA[<p>that is the question&#8230;</p>
<p>I&#8217;m busy working on a JSON based API with client libraries in Flex, C# and Javascript (and eventually iPhone, JavaFX etc).</p>
<p>One of the things the API deals with is the concept of a Market.  Markets have a number of properties that &#8220;belong&#8221; to them, simplistically things like MarketId and Name.</p>

<div id="wp_codebox_msgheader"><span class="right"><a href="javascript:;" onclick="toggle_collapse('p2005');">[<span id="p2005_symbol">-</span>]</a><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p200code5'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div id="wp_codebox"><table width="100%" ><tr id="p2005"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p200code5"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Market
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> MarketId <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>However, I can see that in using a list of markets, its going to be useful to know related data about the market for things like sorting. For example, when showing a list of markets I&#8217;d like to know the volume traded in the last day so I can colour them differently.  Or the price change in the last hour etc.</p>
<p>DTO wise, there seem to be several options.</p>
<ol>
<li>
<h4>Flattening with &#8220;nulls&#8221;</h4>

<div id="wp_codebox_msgheader"><span class="right"><a href="javascript:;" onclick="toggle_collapse('p2006');">[<span id="p2006_symbol">-</span>]</a><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p200code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div id="wp_codebox"><table width="100%" ><tr id="p2006"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p200code6"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Market
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> MarketId <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span><span style="color: #008000;">?</span> VolumeInLastDay <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span><span style="color: #008000;">?</span> PriceDeltaLastHour <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>It seems that over time this approach will lead to very &#8220;messy&#8221; DTOs where you&#8217;re forced to do a lot of &#8220;isNull&#8221; style checking before attempting to display things.
</li>
<li>
<h4>Multiple specific MarketWithXYZ DTOS</h4>

<div id="wp_codebox_msgheader"><span class="right"><a href="javascript:;" onclick="toggle_collapse('p2007');">[<span id="p2007_symbol">-</span>]</a><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p200code7'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div id="wp_codebox"><table width="100%" ><tr id="p2007"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p200code7"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MarketWithVolume<span style="color: #008000;">:</span> Market
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> VolumeInLastDay <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MarketWithPriceDelta<span style="color: #008000;">:</span> Market
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span> PriceDeltaLastHour <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MarketWithVolumeAndPriceDelta<span style="color: #008000;">:</span> MarketWithVolume
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">decimal</span><span style="color: #008000;">?</span> PriceDeltaLastHour <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>An explosion of DTOs, but at least you know exactly what data you&#8217;ve got at any instance
</li>
<li>
<h4>Property Bags</h4>

<div id="wp_codebox_msgheader"><span class="right"><a href="javascript:;" onclick="toggle_collapse('p2008');">[<span id="p2008_symbol">-</span>]</a><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p200code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div id="wp_codebox"><table width="100%" ><tr id="p2008"><td width="1%" class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p200code8"><pre class="csharp"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Market
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> MarketId <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> Hashtable RelatedData <span style="color: #000000;">&#123;</span>get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #FF0000;">decimal</span> delta <span style="color: #008000;">=</span> Market.<span style="color: #0000FF;">RelatedData</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;PriceDeltaLastHour &quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>In a way this feels even worse than having multiple null attributes; especially for someone new to the API because you won&#8217;t know until run time which data is available.
</li>
</ol>
<p>How have you approached this problem in the past?  What works, and what doesn&#8217;t?  Comments much appreciated.</p>
<p><strong>Update</strong></p>
<ul>
<li>Reduced duplication in Multiple specific option using inheritance</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2010/04/20/dtos-to-flatten-or-not-to-flatten/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Utils anti-pattern &amp; AutoMapper</title>
		<link>http://davidlaing.com/2009/06/02/utils-anti-pattern-automapper/</link>
		<comments>http://davidlaing.com/2009/06/02/utils-anti-pattern-automapper/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 09:18:41 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Code smells]]></category>
		<category><![CDATA[automapper]]></category>
		<category><![CDATA[utils-anti-pattern]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=190</guid>
		<description><![CDATA[These are more for my reference purposes &#8211; hopefully you&#8217;ll find them useful:
If you&#8217;re about to name a class **Util; think harder &#8211; there is a better name that discribes what that class is for:
Chriss Missal has some advice for you
Faced with the prospect of heaps of left hand side => right hand side code [...]]]></description>
			<content:encoded><![CDATA[<p>These are more for my reference purposes &#8211; hopefully you&#8217;ll find them useful:</p>
<p>If you&#8217;re about to name a class **Util; think harder &#8211; there is a better name that discribes what that class is for:<br />
<a href="http://www.lostechies.com/blogs/chrismissal/archive/2009/06/01/anti-patterns-and-worst-practices-utils-class.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.lostechies.com/blogs/chrismissal/archive/2009/06/01/anti-patterns-and-worst-practices-utils-class.aspx');">Chriss Missal has some advice for you</a></p>
<p>Faced with the prospect of heaps of left hand side => right hand side code in your DTO of anti-corruption layer?  Consider <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/01/22/automapper-the-object-object-mapper.aspx');">Jimmy Bogard&#8217;s Automapper</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2009/06/02/utils-anti-pattern-automapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Simplest thing that could Possibly work</title>
		<link>http://davidlaing.com/2008/06/27/the-simplest-thing-that-could-possibly-work/</link>
		<comments>http://davidlaing.com/2008/06/27/the-simplest-thing-that-could-possibly-work/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 12:57:26 +0000</pubDate>
		<dc:creator>mrdavidlaing</dc:creator>
				<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://davidlaing.com/?p=67</guid>
		<description><![CDATA[Jay has a good discussion concerning the design dilemma we often face considering the &#8220;simplest thing that could possible work&#8221;.  Is it appropriate to implement a simple half baked solution that we know we will throw away as requirements become more defined?  Or should we start out with a more complex pattern that [...]]]></description>
			<content:encoded><![CDATA[<p>Jay has a good discussion concerning the design dilemma we often face considering the &#8220;simplest thing that could possible work&#8221;.  Is it appropriate to implement a simple half baked solution that we know we will throw away as requirements become more defined?  Or should we start out with a more complex pattern that will give us more flexibility?</p>
<p><a href="http://blog.jayfields.com/2008/06/simplest-thing-that-could-possibly-work.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://blog.jayfields.com/2008/06/simplest-thing-that-could-possibly-work.html');">http://blog.jayfields.com/2008/06/simplest-thing-that-could-possibly-work.html</a></p>
<p>Jay concludes that you can&#8217;t decide which pattern will give you the right flexibility until you have more requirements.  So, rather than potentially implement the <strong>wrong</strong> complex solution, just treat the temporary solution as part of the learning process.</p>
<p>I agree.  Like optimising, you need to know the precise requirements before you can choose the right pattern.</p>
<p>Throw away code isn’t wasted; rather its used like scrap paper in the learning process.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidlaing.com/2008/06/27/the-simplest-thing-that-could-possibly-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
