<?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>Barry Jones &#187; Automatically Populate DTOs &#8211; Barry Jones</title>
	<atom:link href="http://www.barryjones.me.uk/tag/dto/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barryjones.me.uk</link>
	<description>General Ramblings</description>
	<lastBuildDate>Mon, 06 Sep 2010 10:05:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Automatically Populate DTOs</title>
		<link>http://www.barryjones.me.uk/2009/10/automatically-populate-dtos/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=automatically-populate-dtos</link>
		<comments>http://www.barryjones.me.uk/2009/10/automatically-populate-dtos/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 20:07:09 +0000</pubDate>
		<dc:creator>Barry Jones</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[DTO]]></category>
		<category><![CDATA[Pattern]]></category>

		<guid isPermaLink="false">http://carl.barryjones.me.uk/?p=15</guid>
		<description><![CDATA[The lowly DTO, a pattern described by Martin Fowler that enables you to reduce the number of API calls you make across a remote boundary; it allows this by grouping all the data in to a new data type and returning that instead. I have however found that this pattern can cause you to write [...]]]></description>
			<content:encoded><![CDATA[<p>The lowly <a title="DTO Pattern" href="http://martinfowler.com/eaaCatalog/dataTransferObject.html"><acronym title="Data Transfer Object">DTO</acronym></a>, a pattern described by <a title="Martin Fowler" href="http://martinfowler.com/" target="_blank">Martin Fowler</a> that enables you to reduce the number of API calls you make across a remote boundary; it allows this by grouping all the data in to a new data type and returning that instead. I have however found that this pattern can cause you to write a lot of code that is nothing more than property setting. The code provided in this post (<a href="http://www.barryjones.me.uk/wp-content/uploads/2009/10/BarryJones.DataTransferObjects.zip">here</a>) allows you to automate the population of these <acronym title="Data Transfer Object">DTO</acronym>s saving you time, so you can get on with something more important like drinking coffee.<span id="more-15"></span></p>
<h2>Automating DTO Population</h2>
<p>First of all grab yourself a copy of the <a href="http://www.barryjones.me.uk/wp-content/uploads/2009/10/BarryJones.DataTransferObjects.zip">source</a> code. A general overview of the process is; you specify — using attributes — the mappings between your business objects and your data transfer objects. These attributes can describe simple properties, complex properties and collections. After that is done, throw the object at the converter and Bob&#8217;s your uncle!</p>
<h3>Mapping DTO Properties with Attributes</h3>
<p>There are two attributes DTOSimpleMapper and DTOCollectionMapper; the basic signature of the attributes is shown below.</p>
<pre class="code">[DTOSimpleMapper(params string[] fields)]
[DTOCollectionMapper(Type ofType, string fromField)]</pre>
<pre class="code">[DTOSimpleMapper("FromProperty.Child.Identifier")]
public int ConvertedProperty;</pre>
<p>The ‘fields’ and ‘fromField’ parameters describe the property on the origin object that is to be converted. The destination for the conversion is always the property the attribute is decorating. The fields are represented as a string where the field is a child property the ‘.’ notation can be used to indicate this. I.e. &#8220;Parent.Child.Child&#8221;.<br />
The params string[] parameters on both attributes can be used to describe more complex conversions, when the converting types have multiple properties the conversion can be described by passing in an array of strings which define the from and to properties. I.e:</p>
<pre class="code">[DTOSimpleMapper("Prop1.Prop2=Identifier", "Prop2.Prop1=Name")]</pre>
<p>The first parameter on the DTOCollectionMapper tells the converter what the destination array elements are:</p>
<pre class="code">[DTOCollectionMapper(typeof(string), "FromProperty"]
public string[] ConvertedCollection;</pre>
<p>The converter will work with any collection that implements IEnumerable. As with the simple mapper, the property name can describe more complex conversions. This is useful where you have a generic <acronym title="Data Transfer Object">DTO</acronym> that is used to describe many elements:</p>
<pre class="code">[DTOCollectionMapper(typeof(IdentifiedElement), "FromPoperty", "Identifier=Identifier",
"Child.Name=Name")]
public List Children;</pre>
<p>Finally where the conversions are more complex the type for the property can describe its own conversion:</p>
<pre class="code">Public class RelatedChildDTO {
     [DTOSimpleMapper("Identifier")] public int Identifier;
     [DTOSimpleMapper("Name")] public string Name;
}
Public class ParentDTO {
     [DTOSimpleMapper("Child")]
     public RelatedChildDTO Child;
}</pre>
<p>With the above example the converter will check the type of instance it is creating and use the attributes described on that type to perform the conversion.</p>
<h2>Conditional Population</h2>
<p>Both of the attributes allow for Type conditional conversions as well, both are overloaded with a type property that is checked in the conversion process and only performed when the origin type is the same:</p>
<pre class="code">[DTOSimpleMapper(typeof(ConditionalBusinessObject), "FromProperty")]
public string ConvertedProperty;</pre>
<h2>Automatic Conversion</h2>
<p>Now you have described the way objects are converted we are left with actual conversion, the library has a Converter class with a simple static method called Convert. This method takes an instance of an origin object and a type for the destination object.</p>
<pre class="code">BusinessObject bus = new BusinessObject();
// set business properties
DTO dto = (DTO)Convertor.Convert(bus, typeof(DTO));</pre>
<p>The result of this method call is that the <acronym title="Data Transfer Object">DTO</acronym> is created and populated using the attributes, the conversion is done!<br />
Here is the <a href="http://www.barryjones.me.uk/wp-content/uploads/2009/10/BarryJones.DataTransferObjects.zip">source code</a>, it contains test classes which should give you a better idea of how it all works, enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.barryjones.me.uk/2009/10/automatically-populate-dtos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

