<?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>Elegant Code &#187; AutoMapper</title>
	<atom:link href="http://elegantcode.com/category/automapper/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 04:40:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Code Cast 33 &#8211; Jimmy Bogard on AutoMapper</title>
		<link>http://elegantcode.com/2009/10/29/code-cast-33-jimmy-bogard-on-automapper/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=code-cast-33-jimmy-bogard-on-automapper</link>
		<comments>http://elegantcode.com/2009/10/29/code-cast-33-jimmy-bogard-on-automapper/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 05:24:32 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[AutoMapper]]></category>
		<category><![CDATA[CodeCast]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/10/29/code-cast-33-jimmy-bogard-on-automapper/</guid>
		<description><![CDATA[AutoMapper is one those tools tools that seems to be gaining in popularity lately, generating a lot of talk on the web and at user groups.&#160; Seemed like a good idea to talk with its creator, Jimmy Bogard, and get the run-down on it. In this code case we have Elegant Coders Chris Brandsma and [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codeplex.com/AutoMapper">AutoMapper</a> is one those tools tools that seems to be gaining in popularity lately, generating a lot of talk on the web and at user groups.&#160; Seemed like a good idea to talk with its creator, <a href="http://www.lostechies.com/blogs/jimmy_bogard/">Jimmy Bogard</a>, and get the run-down on it.</p>
<p>In this code case we have Elegant Coders <a href="http://elegantcode.com/about/chris-brandsma/">Chris Brandsma</a> and <a href="http://elegantcode.com/about/richard-cirerol/">Richard Cirerol</a> (listen as Chris completely stumbles over Rich’s last name), and we are joined by Cory Isakson.&#160; For those of you outside of Bosie, Cory is a local usergroup leader, Code Camp presenter, and .net junky.&#160; We keep trying to get Cory to blog, but so far he is resisting our calls.</p>
<ul>
<li><a href="http://www.lostechies.com/blogs/jimmy_bogard/">Jimmy’s Blog</a></li>
<li><a href="http://www.codeplex.com/AutoMapper">AutoMapper</a></li>
<li><a href="http://structuremap.sourceforge.net/Default.htm">StructureMap</a></li>
</ul>
<p><a href="http://pluralsight-elegant.s3.amazonaws.com/ECC_33_JimmyBogard.mp3">Get the show here</a></p>
<p><a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=271207118"><img border="0" alt="View in iTunes" src="http://elegantcode.com/cast/files/images/itunes_button.gif" /></a> <a href="http://feeds2.feedburner.com/elegantcodecast"><img border="0" alt="Any Podcatcher" src="http://elegantcode.com/cast/files/images/rss_podcast.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/10/29/code-cast-33-jimmy-bogard-on-automapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://pluralsight-elegant.s3.amazonaws.com/ECC_33_JimmyBogard.mp3" length="36496077" type="audio/mpeg" />
		</item>
		<item>
		<title>Mapping From IDataReader/IDataRecord with AutoMapper</title>
		<link>http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mapping-from-idatareaderidatarecord-with-automapper</link>
		<comments>http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 20:30:35 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[AutoMapper]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/</guid>
		<description><![CDATA[A while ago, I submitted a patch to AutoMapper that added basic support for mapping data from an IDataReader/IDataRecord to an object. For those of us who don&#8217;t have the luxury to use NHibernate in their projects, this feature can save you from writing lots of repetitive and tedious code. Its usage is pretty much [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I submitted a patch to <a href="http://automapper.codeplex.com/" target="_blank">AutoMapper</a> that added basic support for mapping data from an <a href="http://msdn.microsoft.com/en-us/library/system.data.idatareader.aspx" target="_blank">IDataReader</a>/<a href="http://msdn.microsoft.com/en-us/library/system.data.idatarecord.aspx" target="_blank">IDataRecord</a> to an object. For those of us who don&#8217;t have the luxury to use NHibernate in their projects, this feature can save you from writing lots of repetitive and tedious code. </p>
<p>Its usage is pretty much the same as with regular object-to-object mapping using AutoMapper. Lets show a very simple example.</p>
<p>Suppose we have a view object like the one shown below:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeView
{
    <span class="kwrd">public</span> Int32 SomeNumber { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Guid AnId { get; <span class="kwrd">private</span> set; }
    <span class="kwrd">public</span> Double OrNothing { get; <span class="kwrd">private</span> set; }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Now when we can execute a query like this,</p>
<pre class="csharpcode"><span class="kwrd">SELECT</span> ColumnA <span class="kwrd">AS</span> SomeNumber,
       ColumnB <span class="kwrd">AS</span> AnId,
       ColumnC <span class="kwrd">AS</span> OrNothing
<span class="kwrd">FROM</span> SomeTable
<span class="kwrd">WHERE</span> ...</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>and read the results using a data reader. Now we can use AutoMapper to map the results to instances of our view class:</p>
<pre class="csharpcode">var dataReader = ... <span class="rem">// Execute a data reader</span>
var views = Mapper.Map&lt;IDataReader, IEnumerable&lt;SomeView&gt;&gt;(_dataReader);</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>This results in a collection of one of or more view objects. When our query is guaranteed to always return one record, we can use the following syntax:</p>
<pre class="csharpcode">var dataRecord = ... <span class="rem">// Execute data reader and read first record</span>
var = Mapper.Map&lt;IDataRecord, SomeView&gt;(_dataRecord);</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>This approach expects that a convention is followed whereby the name of a field returned by the query matches the name of a property on the target class. Its also possible to use <a href="http://automapper.codeplex.com/wikipage?title=Projection&amp;referringTitle=Home" target="_blank">projection</a> as already provided for regular object-to-object mapping.</p>
<p>Suppose we add a new property to our view,</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SomeView
{
    ...
    <span class="kwrd">public</span> DateTime SomeDate { get; <span class="kwrd">private</span> set; }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>and we modify the query so that we retrieve the corresponding date value from the database:</p>
<pre class="csharpcode"><span class="kwrd">SELECT</span> ...
       ColumnD <span class="kwrd">AS</span> BirthDay
<span class="kwrd">FROM</span> SomeTable</pre>
<p>Notice that we&#8217;ve broken the convention here and we need to use projection to ensure that the retrieved date value is mapped to the correct property.</p>
<pre class="csharpcode">Mapper.CreateMap&lt;IDataRecord, SomeView&gt;()
    .ForMember(dest =&gt; dest.SomeDateAndTime,
               options =&gt; options.MapFrom(
               src =&gt; src.GetDateTime(src.GetOrdinal(<span class="str">"BirthDay"</span>))));

var dataRecord = ...    <span class="rem">// Execute data reader and read first record</span>
var = Mapper.Map&lt;IDataRecord, SomeView&gt;(_dataRecord);</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Using projection we&#8217;re able to manually map from a data reader or data record. In some sense,&nbsp; we&#8217;re back to square one if we have to do this for all fields. Trying to follow the convention is of course the most useful.&nbsp; </p>
<p>I know it&#8217;s not much, but I think it can be helpful for those cases where you actually need to map from a data reader or a data record to an object.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

