<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: The case for two-way mapping in AutoMapper</title>
	<atom:link href="http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Mon, 17 Jun 2013 21:27:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Saulo Vallory</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4914</link>
		<dc:creator>Saulo Vallory</dc:creator>
		<pubDate>Thu, 06 Sep 2012 03:33:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4914</guid>
		<description>Maybe I&#039;m missing the point here. I don&#039;t know. I&#039;m using &quot;two-way&quot; mapping by creating two mapping configurations (Entity =&gt; DTO and DTO =&gt; Entity). They have different rules and I set up AutoMapper to ignore the already existing related entities, while setting their ids to my exposed foreign key. I&#039;m even mapping a comma separated list of words to a Tag collection, since in my schema tags aren&#039;t reused even when they have the same name. 

Look at my mapping code...

Mapper.CreateMap()    .ForMember(d =&gt; d.Tags, opt =&gt; opt.MapFrom(d =&gt; d.Tags.Aggregate(&quot;&quot;, (total, tag) =&gt; total + (tag.Name + &quot;, &quot;)).Trim(&#039; &#039;,&#039;,&#039;)));
Mapper.CreateMap()    .ForMember(d =&gt; d.Competition, opt =&gt; opt.Ignore())    .ForMember(d =&gt; d.Tags, opt =&gt; opt.MapFrom(d =&gt; d.Tags.Split(&#039;,&#039;).Select(name =&gt; new Tag() { Name = name.Trim() })));


gist: https://gist.github.com/3650672

See, a design is submitted to a competition, so the competition already exists. If I allow 
AutoMapper to map the competition property, EF would try to insert a new Competition, 
so I have to ignore it and only set the foreign key. Of course, I have validation on the 
DTO and on my Entity.

Is there anything wrong with this approach?</description>
		<content:encoded><![CDATA[<p>Maybe I&#8217;m missing the point here. I don&#8217;t know. I&#8217;m using &#8220;two-way&#8221; mapping by creating two mapping configurations (Entity =&gt; DTO and DTO =&gt; Entity). They have different rules and I set up AutoMapper to ignore the already existing related entities, while setting their ids to my exposed foreign key. I&#8217;m even mapping a comma separated list of words to a Tag collection, since in my schema tags aren&#8217;t reused even when they have the same name. </p>
<p>Look at my mapping code&#8230;</p>
<p>Mapper.CreateMap()    .ForMember(d =&gt; d.Tags, opt =&gt; opt.MapFrom(d =&gt; d.Tags.Aggregate(&#8220;&#8221;, (total, tag) =&gt; total + (tag.Name + &#8220;, &#8220;)).Trim(&#8216; &#8216;,&#8217;,')));<br />
Mapper.CreateMap()    .ForMember(d =&gt; d.Competition, opt =&gt; opt.Ignore())    .ForMember(d =&gt; d.Tags, opt =&gt; opt.MapFrom(d =&gt; d.Tags.Split(&#8216;,&#8217;).Select(name =&gt; new Tag() { Name = name.Trim() })));</p>
<p>gist: <a href="https://gist.github.com/3650672" rel="nofollow">https://gist.github.com/3650672</a></p>
<p>See, a design is submitted to a competition, so the competition already exists. If I allow<br />
AutoMapper to map the competition property, EF would try to insert a new Competition,<br />
so I have to ignore it and only set the foreign key. Of course, I have validation on the<br />
DTO and on my Entity.</p>
<p>Is there anything wrong with this approach?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: How to mutate editmodel/postmodel to domain model</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4663</link>
		<dc:creator>How to mutate editmodel/postmodel to domain model</dc:creator>
		<pubDate>Thu, 05 Jul 2012 10:38:43 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4663</guid>
		<description>[...] is also described in Jimmy Bogards article: The case for two-way mapping in AutoMapper, but the solution to this isn&#8217;t described in detail, only that they [...]</description>
		<content:encoded><![CDATA[<p>[...] is also described in Jimmy Bogards article: The case for two-way mapping in AutoMapper, but the solution to this isn&#8217;t described in detail, only that they [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Domain model to ViewModel and back again using repository pattern and Entity Framework in MVC3 &#124; PHP Developer Resource</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4614</link>
		<dc:creator>Domain model to ViewModel and back again using repository pattern and Entity Framework in MVC3 &#124; PHP Developer Resource</dc:creator>
		<pubDate>Wed, 23 May 2012 23:50:02 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4614</guid>
		<description>[...] have read here http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/ about how you probably shouldn&#8217;t be trying to un-flatten a flattened object, but considering [...]</description>
		<content:encoded><![CDATA[<p>[...] have read here <a href="http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/" rel="nofollow">http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/</a> about how you probably shouldn&#8217;t be trying to un-flatten a flattened object, but considering [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ted Driggs</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4576</link>
		<dc:creator>Ted Driggs</dc:creator>
		<pubDate>Wed, 02 May 2012 18:57:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4576</guid>
		<description>On further examination, I think you&#039;re right not to support it - there shouldn&#039;t be enough places where the EditModel to Entity conversion is taking place to make the mapping worthwhile. 

For now I&#039;ve refactored that conversion process out into a method in the entity&#039;s controller which also validates the EditModel. Not sure if this is a great idea, but I need to do validation that can&#039;t be expressed in data annotations and this ensures only valid EditModels ever even become entities.</description>
		<content:encoded><![CDATA[<p>On further examination, I think you&#8217;re right not to support it &#8211; there shouldn&#8217;t be enough places where the EditModel to Entity conversion is taking place to make the mapping worthwhile. </p>
<p>For now I&#8217;ve refactored that conversion process out into a method in the entity&#8217;s controller which also validates the EditModel. Not sure if this is a great idea, but I need to do validation that can&#8217;t be expressed in data annotations and this ensures only valid EditModels ever even become entities.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4570</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 01 May 2012 15:54:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4570</guid>
		<description>You can certainly use AutoMapper if you like - it just doesn&#039;t work well for collection types (nothing really does), nor do you want to change your entity to better support this behavior. Try it out, and if things get weird/difficult, just do it manually.</description>
		<content:encoded><![CDATA[<p>You can certainly use AutoMapper if you like &#8211; it just doesn&#8217;t work well for collection types (nothing really does), nor do you want to change your entity to better support this behavior. Try it out, and if things get weird/difficult, just do it manually.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ted Driggs</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4567</link>
		<dc:creator>Ted Driggs</dc:creator>
		<pubDate>Tue, 01 May 2012 14:54:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4567</guid>
		<description>What then is the recommended way to take in an edit model and save it back to the database? Currently I validate the incoming edit model, then once the data it contains is known to be good I want to convert it to an entity and save it as painlessly as possible (it&#039;s not out of the question that the schema of these objects may change later, so a solution like AutoMapper would be better than hand-writing the constructor for the entity.</description>
		<content:encoded><![CDATA[<p>What then is the recommended way to take in an edit model and save it back to the database? Currently I validate the incoming edit model, then once the data it contains is known to be good I want to convert it to an entity and save it as painlessly as possible (it&#8217;s not out of the question that the schema of these objects may change later, so a solution like AutoMapper would be better than hand-writing the constructor for the entity.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Diego H. Bustamante</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4354</link>
		<dc:creator>Diego H. Bustamante</dc:creator>
		<pubDate>Wed, 07 Mar 2012 02:24:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4354</guid>
		<description>I&#039;m in agreement that you shouldn&#039;t map a DTO or Model (from mvc) back into an Entity... however, I&#039;m using the mapping in order to generate query builder (entity sql) statements from my models (implemented in jqGrid  [Lib.Web.MVC]) that can execute in my DAL againsts Entities... of course if i was a LINQ Expressions pro, I&#039;d circumvent this altogether, yet i&#039;d still need some sort of mapping.</description>
		<content:encoded><![CDATA[<p>I&#8217;m in agreement that you shouldn&#8217;t map a DTO or Model (from mvc) back into an Entity&#8230; however, I&#8217;m using the mapping in order to generate query builder (entity sql) statements from my models (implemented in jqGrid  [Lib.Web.MVC]) that can execute in my DAL againsts Entities&#8230; of course if i was a LINQ Expressions pro, I&#8217;d circumvent this altogether, yet i&#8217;d still need some sort of mapping.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Tuliper</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4327</link>
		<dc:creator>Adam Tuliper</dc:creator>
		<pubDate>Wed, 15 Feb 2012 09:15:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4327</guid>
		<description>Hi Jimmy - Did you ever dig anything up here? I have to agree with Jag here. We all know AutoMapper is commonly used to go from Entity-&gt;ViewModel. Logic would then say it has nothing to do with laziness, but cleaner controllers to map our properties back to an entity. This fits well with say Entity Framework as we can load up an entity, merge its properties, mark it as modified (since AutoMappers changes dont get picked up by the context) and save it, all with minimal code. Why would this not be a case for usage of a field mapper?  Ok, I guess we could write smart templates to script this out, but if we&#039;re already going &#039;one way&#039; why not back the other? The pattern you&#039;ve discussed with MVC I have yet to see, so I&#039;m curious of some examples. Thanks!!</description>
		<content:encoded><![CDATA[<p>Hi Jimmy &#8211; Did you ever dig anything up here? I have to agree with Jag here. We all know AutoMapper is commonly used to go from Entity-&gt;ViewModel. Logic would then say it has nothing to do with laziness, but cleaner controllers to map our properties back to an entity. This fits well with say Entity Framework as we can load up an entity, merge its properties, mark it as modified (since AutoMappers changes dont get picked up by the context) and save it, all with minimal code. Why would this not be a case for usage of a field mapper?  Ok, I guess we could write smart templates to script this out, but if we&#8217;re already going &#8216;one way&#8217; why not back the other? The pattern you&#8217;ve discussed with MVC I have yet to see, so I&#8217;m curious of some examples. Thanks!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen M. Redd</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4101</link>
		<dc:creator>Stephen M. Redd</dc:creator>
		<pubDate>Wed, 16 Nov 2011 10:34:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4101</guid>
		<description>The whole CommandMessage thing threw me off a lot... so I did some digging... lots of digging. 

If I&#039;m correct though, and I might not be, what Jimmy was talking about is a pattern that is very similar to what &lt;a href=&quot;http://codecampserver.codeplex.com&quot; rel=&quot;nofollow&quot;&gt;CodeCameServer&lt;/a&gt; uses, which involves the  MVCContrib.CommandProcessor.     

I&#039;m still digging through the details, but generally it looks like all updates are done via calls to a rules engine. You give it a message object, and the rules engine can tell from the message object&#039;s type which command it needs to run.

Setting up the rules engine involves creating custom classes that define the command, a class to define the rules to apply, classes for any custom rules, and a class that defines the actual message that will be passed around.... an outright obscene number of classes actually. 

It seems to be overkill times about a million, and I cannot justify this level of complexity in an app the size of CodeCampeServer. It would seem like a simple transaction script design  pattern would probably have done just fine, and been a lot smaller and easier to maintain. Maybe a rules engine like this makes sense given different requirements, or at a much larger scale (though I&#039;d have to see details to agree).

Still though, the general idea is that the domain would expose very narrowly defined operations/commands for any writes to the DB. When you called those operations/command, you would not be passing in whole entity objects as parameters. Instead, you would pass in objects that are designed specifically for the operation/command being invoked, and back-end domain would then make or modify the appropriate entity objects.  

The command message pattern makes the assumption that only the back-end domain will instantiate or modify the real entities. The front end can read them, but would never change one. 

This is not the dominant pattern used in asp.net MVC, at least not that I&#039;ve seen. Many MVC apps use  entity objects as both input and output to the back-end; and this is especially true when using EF code-first or POCOs. Most of the books, code samples, templates, and scaffolders also use the back-end entity types bidirectionally; even when view models are being used as intermediaries.</description>
		<content:encoded><![CDATA[<p>The whole CommandMessage thing threw me off a lot&#8230; so I did some digging&#8230; lots of digging. </p>
<p>If I&#8217;m correct though, and I might not be, what Jimmy was talking about is a pattern that is very similar to what <a href="http://codecampserver.codeplex.com" rel="nofollow">CodeCameServer</a> uses, which involves the  MVCContrib.CommandProcessor.     </p>
<p>I&#8217;m still digging through the details, but generally it looks like all updates are done via calls to a rules engine. You give it a message object, and the rules engine can tell from the message object&#8217;s type which command it needs to run.</p>
<p>Setting up the rules engine involves creating custom classes that define the command, a class to define the rules to apply, classes for any custom rules, and a class that defines the actual message that will be passed around&#8230;. an outright obscene number of classes actually. </p>
<p>It seems to be overkill times about a million, and I cannot justify this level of complexity in an app the size of CodeCampeServer. It would seem like a simple transaction script design  pattern would probably have done just fine, and been a lot smaller and easier to maintain. Maybe a rules engine like this makes sense given different requirements, or at a much larger scale (though I&#8217;d have to see details to agree).</p>
<p>Still though, the general idea is that the domain would expose very narrowly defined operations/commands for any writes to the DB. When you called those operations/command, you would not be passing in whole entity objects as parameters. Instead, you would pass in objects that are designed specifically for the operation/command being invoked, and back-end domain would then make or modify the appropriate entity objects.  </p>
<p>The command message pattern makes the assumption that only the back-end domain will instantiate or modify the real entities. The front end can read them, but would never change one. </p>
<p>This is not the dominant pattern used in asp.net MVC, at least not that I&#8217;ve seen. Many MVC apps use  entity objects as both input and output to the back-end; and this is especially true when using EF code-first or POCOs. Most of the books, code samples, templates, and scaffolders also use the back-end entity types bidirectionally; even when view models are being used as intermediaries.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/#comment-4066</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 02 Nov 2011 18:37:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/09/17/the-case-for-two-way-mapping-in-automapper.aspx#comment-4066</guid>
		<description>Yeah, I think you&#039;re right. We used to have a code sample, but the context was so simplified that it didn&#039;t make sense that the pattern was even used. I&#039;ll see if I can dig something up.</description>
		<content:encoded><![CDATA[<p>Yeah, I think you&#8217;re right. We used to have a code sample, but the context was so simplified that it didn&#8217;t make sense that the pattern was even used. I&#8217;ll see if I can dig something up.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
