Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoMapper using the wrong constructor
    primarykey
    data
    text
    <p>Today I upgraded a fully functioning application using AutoMapper v1.1 to now use AutoMapper v2.1 and I am coming across some issues that I never encountered using the previous version.</p> <p>Here is an example of my code mapping back from <strong>Dto</strong> to <strong>Domain</strong> object</p> <pre><code>public class TypeOne { public TypeOne() { } public TypeOne(TypeTwo two) { //throw ex if two is null } public TypeOne(TypeTwo two, TypeThree three) { //throw ex if two or three are null } public TypeTwo Two {get; private set;} public TypeThree Three {get; private set;} } public class TypeOneDto { public TypeOneDto() { } public TypeTwoDto Two {get; set;} public TypeThreeDto Three {get; set;} } </code></pre> <p>...</p> <pre><code>Mapper.CreateMap&lt;TypeThreeDto, TypeThree&gt;(); Mapper.CreateMap&lt;TypeTwoDto, TypeTwo&gt;(); Mapper.CreateMap&lt;TypeOneDto, TypeOne&gt;(); var typeOne = Mapper.Map&lt;TypeOne&gt;(typeOneDto); </code></pre> <p>However the first problem I encountered with v2.1 was that AutoMapper was trying to use the constructor with 2 args when one of the args was null and should be using the 1 arg constructor.</p> <p>I then tried to use </p> <pre><code>Mapper.CreateMap&lt;TypeOneDto, TypeOne&gt;().ConstructUsing(x =&gt; new TypeOne()); </code></pre> <p>But I kept getting an 'Ambiguous Invocation' error that I couldn't resolve.</p> <p>I then tried</p> <pre><code>Mapper.CreateMap&lt;TypeOneDto, TypeOne&gt;().ConvertUsing(x =&gt; new TypeOne()); </code></pre> <p>and that did successfully create the TypeOne object using the parameterless constructor but then it failed to set the private setter properties.</p> <p>I have looked for help on the AutoMapper website and downloaded the source code to have a good look but didn't get far with the little documentation about and there were not many unit tests for ConstructUsing.</p> <p>Is there anything obvious I am missing that I should change with v2.1? I am surprised that it has changed so much from v1.1.</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.
 

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