<?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>Crafting software&#187; dtos</title>
	<atom:link href="http://davidlaing.com/tag/dtos/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidlaing.com</link>
	<description>David Laing&#039;s thoughts on software development</description>
	<lastBuildDate>Wed, 01 Feb 2012 11:02:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>DTOs: To Flatten Or Not To Flatten?</title>
		<link>http://davidlaing.com/2010/04/20/dtos-to-flatten-or-not-to-flatten/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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. [...]]]></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>
<pre class="brush: csharp; title: ; notranslate">
public class Market
{
    public int MarketId {get; set;}
    public string Name {get; set;}
}
</pre>
<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>
<pre class="brush: csharp; title: ; notranslate">
public class Market
{
    public int MarketId {get; set;}
    public string Name {get; set;}
    public decimal? VolumeInLastDay {get; set;}
    public decimal? PriceDeltaLastHour {get; set;}
}
</pre>
<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>
<pre class="brush: csharp; title: ; notranslate">
public class MarketWithVolume: Market
{
    public decimal VolumeInLastDay {get; set;}
}

public class MarketWithPriceDelta: Market
{
    public decimal PriceDeltaLastHour {get; set;}
}

public class MarketWithVolumeAndPriceDelta: MarketWithVolume
{
    public decimal? PriceDeltaLastHour {get; set;}
}
</pre>
<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>
<pre class="brush: csharp; title: ; notranslate">
public class Market
{
    public int MarketId {get; set;}
    public string Name {get; set;}
    public Hashtable RelatedData {get; set;}
}

decimal delta = Market.RelatedData[&quot;PriceDeltaLastHour &quot;];
</pre>
<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>
	</channel>
</rss>

