4

我想要遵循Jimmy Bogard's advice for automapping through an actionfilter(它在大多數情況下工作很好)。但是如果我有一個自定義視圖模型和我想要映射的集合屬性呢?例如,如何使用自動映射器的ViewModel與IEnumerable <T>屬性

public class WidgetSearchViewModel 
{ 
    public WidgetSearchOptionsViewModel Options { get; set; } 
    public GenericListPagerViewModel Pager { get; set; } 
    public IEnumerable<WidgetSearchResultModel> Results { get; set; } 
} 

results屬性來自於存儲庫作爲域實體的IEnumerable,我要轉換爲使用automapper WidgetSearchResultModel實例。問題是我需要在控制器中構造一個WidgetSearchViewModel來填充Options和Pager屬性。我如何使用AutoMapper ActionFilter來填充結果屬性OnActionExecuted?它甚至是可能的,還是我需要在控制器中放置Mapper.Map調用並引導單元測試中的所有映射?

回答

2

Automapper已經構建支持任何實現IEnumerable的嵌套集之間的映射。如果您定義了父級和子級屬性映射,automapper將會很好地映射嵌套集合。

Automapper也可以處理映射什麼東西,如果你是實現CustomTypeConvertor:http://automapper.codeplex.com/wikipage?title=Custom%20Type%20Converters&referringTitle=Home

+1

此外,單元測試可以調用Mapper.AssertConfigurationIsValid();哪些測試您的配置將工作。 – CRice 2011-01-19 04:32:34