Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Taking from your mapping you could do the following which does not require you to enter all of the property mappings by hand. The helper method in the second test allows for specialized mapping that you may not need.</p> <pre><code>[TestMethod] public void Given_a_wrapper_class_I_should_be_able_to_unwrap_it_and_continue_with_mappings() { Mapper.CreateMap&lt;Wrapper&lt;Source&gt;, Destination&gt;() .UnwrapUsing&lt;Wrapper&lt;Source&gt;, Source, Destination&gt;(w =&gt; w.Model) .ForMember(d =&gt; d.Bar, o =&gt; o.MapFrom(d =&gt; d.Baz)); var source = new Source { Baz = 1, Foo = "One" }; var wrapped = new Wrapper&lt;Source&gt; { Model = source }; var destination = Mapper.Map&lt;Destination&gt;(wrapped); Assert.IsNotNull(destination); Assert.AreEqual(1, destination.Bar); Assert.AreEqual("One", destination.Foo); } public static class AutoMapperExtensions { public static IMappingExpression&lt;TSource, TDest&gt; UnwrapUsing&lt;TWrapper, TSource, TDest&gt;(this IMappingExpression&lt;TWrapper, TDest&gt; expr, Func&lt;Wrapper&lt;TSource&gt;, TSource&gt; unwrapper) { Mapper.CreateMap&lt;Wrapper&lt;TSource&gt;, TDest&gt;() .ConstructUsing(x =&gt; Mapper.Map&lt;TSource, TDest&gt;(unwrapper(x))); return Mapper.CreateMap&lt;TSource, TDest&gt;(); } } public class Source { public string Foo { get; set; } public int Baz { get; set; } } public class Destination { public string Foo { get; set; } public int Bar { get; set; } } public class Wrapper&lt;T&gt; { public T Model { get; set; } } </code></pre>
 

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