4
我讀過無數其他帖子,似乎無法弄清楚發生了什麼,所以現在是時候尋求幫助。自動映射器集合
我試圖將包含集合的域實體映射到也包含集合的dtos。
這是一個簡單的例子; (ⅰ提前的代碼壁道歉,我試圖保持它儘可能短):
實體
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; }
}
地圖
Mapper.CreateMap<Foo, FooDto>();
Mapper.CreateMap<ICollection<Bar>, IEnumerable<BarDto>>();
個
測試
// 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());
我沒有得到任何配置錯誤和Foo1是映射就好了。
o.Bars是Castle.Core.Interceptor.IInterceptor[]
並且不包含任何從我的域實體的價值觀......
我失去的是什麼?
去圖;我知道我忽略了一些簡單的東西。不能給你信用7分鐘。謝謝您的幫助! – 2011-05-07 20:14:47