2017-05-03 125 views
0

有關Automapper的快速新手問題。Automapper,我們可以看到它如何映射對象?

我有以下代碼:

CreateMap<MyDataContract, MyViewModel>() 
    .MapFrom(s => s.Trans, d => d.Trans); 

在DataContract,MyDataContract,我有以下幾點:

[DataMember] 
public IEnumerable<ReferTypeDataContract> Refer { get; set;} 

我試圖找出如何Automapper從上面的CreateMap映射此到ReferTypeDataContract。是否有任何可視化工具來檢查此問題,或者可以添加一些調試代碼以使其可見。

我問的原因是我有一個映射MyDataContract的不同映射,但是我得到了一組不同的Refer列表結果集,並且無法弄清楚。

+0

然後發佈您的代碼。自動映射器按名稱映射字段。您沒有發佈任何顯示'Refer'的使用方法[ –

+0

]這裏是[Source](https://github.com/AutoMapper/AutoMapper),其餘部分由您決定。 – Rabban

+0

所以在這種情況下,它只是一個黑盒子,它自己的東西 – gilesrpa

回答

0

Automapper可以如文檔Understanding your mapping中所述可視化其執行計劃。

這意味着你可以得到任何源 - 目標對的表達式樹。例如:

var configuration = new MapperConfiguration(cfg => {/* your mappings */}); 

LambdaExpression executionPlan = configuration.BuildExecutionPlan(typeof(Foo), typeof(Bar)); 

文檔提供使用此VS extention可視化樹。但是,看起來擴展在Visual Studio 2017中不起作用。在這種情況下讓我們使用內置的文本可視化:

public class Foo 
{ 
    public int Id { get; set; } 
    public List<FooInner> Inners { get; set; } 
} 

創建一個映射配置Foo -> BarFooInner -> BarInner。在BuildExecutionPlan方法被調用後設置一個斷點並在Quick Watch中檢查executionPlanShift+F9)。然後前往DebugView物業,並選擇Text visualizer箭頭在新窗口中打開。通常你會得到很多的文字中包含的語句:

$resolvedValue = .If (
    False || $src == null 
) { 
    .Default(System.Collections.Generic.List`1[XUnitTests.FooInner]) 
} .Else { 
    $src.Inners 
}; 

$passedDestination = .If ($dest == null) { 
    .Default(System.Collections.Generic.List`1[XUnitTests.BarInner]) 
} .Else { 
    $typeMapDestination.Inners 
}; 

因此,它是描述Foo性質如何在細節映射。

相關問題