Trucking Collections

I read countless other posts and can't figure out what's going on, so it's time for some help.

I am trying to display domain objects that contain collections for dtos, also containing collections.

Here's a primitive example; (I apologize in advance for the code wall, I tried to keep it as short as possible):

The objects

public class Foo { public Foo() { Bars = new List<Bar>(); } public string Foo1 { get; set; } public ICollection<Bar> Bars { get; set; } } public class Bar { public string Bar1 { get; set; } } 

Dtos

 public class FooDto { public FooDto() { Bars = new List<BarDto>(); } public string Foo1 { get; set; } public IEnumerable<BarDto> Bars { get; set; } } public class BarDto { public string Bar1 { get; set; } } 

Cards

 Mapper.CreateMap<Foo, FooDto>(); Mapper.CreateMap<ICollection<Bar>, IEnumerable<BarDto>>(); 

Test

 // Arrange var e = new Foo { Foo1 = "FooValue1", Bars = new List<Bar> { new Bar { Bar1 = "Bar1Value1" }, new Bar { Bar1 = "Bar2Value1" } } }; // Act var o = Mapper.Map<Foo, FooDto>(e); // Assert Mapper.AssertConfigurationIsValid(); Assert.AreEqual(e.Foo1, o.Foo1); Assert.IsNotNull(o.Bars); Assert.AreEqual(2, o.Bars.Count()); 

I am not getting any configuration errors, and Foo1 is just fine.

o.Bars is Castle.Core.Interceptor.IInterceptor[] and does not contain any values ​​from my domain object ...

What am I missing here?

+4
source share
1 answer

Instead:

 Mapper.CreateMap<ICollection<Bar>, IEnumerable<BarDto>>(); 

try simply:

 Mapper.CreateMap<Bar, BarDto>(); 

The auto mapper takes care of the rest.

+11
source

All Articles