Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoMapper recursive
    primarykey
    data
    text
    <p>I would like to make a deep copy of a complex object tree using AutoMapper. The problem is that for each member I would like to construct a new object and then map it, and not simply copying the existing one. Here it is an example:</p> <pre><code>public abstract class Test { public Test() { this.Id = Guid.NewGuid(); } public Guid Id { get; private set; } } public class OuterTest : Test { public InnerTest Inner { get; set; } } public class InnerTest : Test { public int Value { get; set; } } </code></pre> <p>and how to test it:</p> <pre><code>OuterTest outerDest = Mapper.Map&lt;OuterTest, OuterTest&gt;(outerSource); System.Diagnostics.Debug.WriteLine("InnerSource id: " + innerSource.Id); System.Diagnostics.Debug.WriteLine("InnerSource value: " + innerSource.Value); System.Diagnostics.Debug.WriteLine("OuterSource id: " + outerSource.Id); System.Diagnostics.Debug.WriteLine("OuterDest id: " + outerDest.Id); System.Diagnostics.Debug.WriteLine("OuterDest.Inner id: " + outerDest.Inner.Id); System.Diagnostics.Debug.WriteLine("OuterDest.Inner value: " + outerDest.Inner.Value); </code></pre> <p>This is the result from the output window:</p> <pre><code>InnerSource id: a60fda37-206a-40a8-a7f8-db480149c906 InnerSource value: 2119686684 OuterSource id: 7486899e-2da8-4873-9160-d6096b555c73 OuterDest id: 7486899e-2da8-4873-9160-d6096b555c73 OuterDest.Inner id: a60fda37-206a-40a8-a7f8-db480149c906 OuterDest.Inner value: 2119686684 </code></pre> <p>The problem is thet the object innerSource is always the same instance as outerDest.Inner (I verified through MakeObjectId of VS debugger) but I would like them to be two different instances.</p> <p>How could I create a recursive map with this behavior? I tried creating a custom IValueResolver like the following, without success.</p> <pre><code> public class AutoMapperNewObjectResolver : IValueResolver { public ResolutionResult Resolve(ResolutionResult source) { object resolved; if (source.Value != null) { object instance = Activator.CreateInstance(source.MemberType); resolved = Mapper.Map(source.Value, instance, source.MemberType, source.MemberType); } else { resolved = null; } ResolutionResult result = source.New(resolved, source.Context.DestinationType); return result; } } </code></pre> <p>and configured like this:</p> <pre><code>Mapper.CreateMap&lt;OuterTest, OuterTest&gt;() .ForMember(d =&gt; d.Inner, o =&gt; o.ResolveUsing&lt;AutoMapperNewObjectResolver&gt;().FromMember(src =&gt; src.Inner)); </code></pre> <p>Any help appreciated, thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload