Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While Automapper is not designed with this in mind, it's powerful enough to let you do it, using <a href="https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters" rel="nofollow">custom type converters</a>. You need to create your own converter from <code>IList&lt;House&gt;</code> to <code>IList&lt;HouseDto&gt;</code>, and inject it using a factory:</p> <pre><code>using System; using System.Collections.Generic; using AutoMapper; using NUnit.Framework; using SharpTestsEx; namespace StackOverflowExample { public class House { public string Address { get; set; } } public class Person { public IList&lt;House&gt; OwnedHouse { get; set; } } public class HouseDto { public string Address { get; set; } } public class PersonDto { public IList&lt;HouseDto&gt; OwnedHouse { get; set; } } [TestFixture] public class AutomapperTest { public interface IHouseListConverter : ITypeConverter&lt;IList&lt;House&gt;, IList&lt;HouseDto&gt;&gt; { } public class HouseListConverter : IHouseListConverter { private readonly IDictionary&lt;House, HouseDto&gt; existingMappings; public HouseListConverter(IDictionary&lt;House, HouseDto&gt; existingMappings) { this.existingMappings = existingMappings; } public IList&lt;HouseDto&gt; Convert(ResolutionContext context) { var houses = context.SourceValue as IList&lt;House&gt;; if (houses == null) { return null; } var dtos = new List&lt;HouseDto&gt;(); foreach (var house in houses) { HouseDto mapped = null; if (existingMappings.ContainsKey(house)) { mapped = existingMappings[house]; } else { mapped = Mapper.Map&lt;HouseDto&gt;(house); existingMappings[house] = mapped; } dtos.Add(mapped); } return dtos; } } public class ConverterFactory { private readonly IHouseListConverter resolver; public ConverterFactory() { resolver = new HouseListConverter(new Dictionary&lt;House, HouseDto&gt;()); } public object Resolve(Type t) { return t == typeof(IHouseListConverter) ? resolver : null; } } [Test] public void CustomResolverTest() { Mapper.CreateMap&lt;House, HouseDto&gt;(); Mapper.CreateMap&lt;IList&lt;House&gt;, IList&lt;HouseDto&gt;&gt;().ConvertUsing&lt;IHouseListConverter&gt;(); Mapper.CreateMap&lt;Person, PersonDto&gt;(); var house = new House {Address = "any"}; var john = new Person {OwnedHouse = new List&lt;House&gt; {house}}; var will = new Person { OwnedHouse = new List&lt;House&gt; { house } }; var converterFactory = new ConverterFactory(); var johnDto = Mapper.Map&lt;PersonDto&gt;(john, o=&gt;o.ConstructServicesUsing(converterFactory.Resolve)); var willDto = Mapper.Map&lt;PersonDto&gt;(will, o=&gt;o.ConstructServicesUsing(converterFactory.Resolve)); johnDto.OwnedHouse[0].Should().Be.SameInstanceAs(willDto.OwnedHouse[0]); johnDto.OwnedHouse[0].Address.Should().Be("any"); } } } </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