2010-01-24 123 views
18

我想知道是否可以使用Automapper將多個DTO對象映射到單個ViewModel對象?是否可以使用Automapper將多個DTO對象映射到單個ViewModel?

本質上,我有多個DTO對象,並希望在ASP.NET MVC 2.0的單個屏幕上顯示來自每個對象的信息。爲此,我想將DTO對象(或其中的一部分...)平鋪到Viewmodel中,並將所述視圖模型傳遞給視圖。如果我有一個DTO,這很容易,但我從來沒有看到它與多個。很明顯,有很多迂迴的方法可以做到這一點(automapper之外),但如果可能的話,這是我想採取的方法。

+0

您可以通過ValueInjecter http://valueinjecter.codeplex.com/documentation – Omu 2010-05-05 18:28:06

回答

7

您可以創建一個包含兩個或多個DTO對象並將複合DTO映射到輸出視圖模型的複合DTO。

+1

這就是我們所做的,我發現它的效果最好。然後,該複合表示對象傾向於開始包含與複合對象有關的行爲等。 – 2010-01-25 13:33:32

+2

我開始沿着這條路線前進,但後來認識到,如果在我可以Map <>()之前必須手動映射到一個複合對象,那麼給我提供的額外步驟是什麼?遵循這種方法,你確保所有的對象映射都是通過AutoMapper進行管理的,但是除了手動映射到組合之外。我並不完全反對這個想法,只是說... – tbehunin 2010-10-19 17:58:49

+0

同意。 Gordon Nongkynrih剛剛發佈的解決方案對我來說看起來更加優雅。 – 2012-01-19 08:45:01

13
+0

雖然在這個線程中很晚,但是非常感謝你的這個鏈接。我剛開始使用AutoMapper,很快就會遇到這個問題!給予好評! :-) – 2012-01-19 08:41:22

+0

謝謝。我知道它在線程中很晚,但最近面臨同樣的問題。我意識到它永遠不會遲到更新甚至老的線程:) – Bahdeng 2012-01-22 08:18:37

+5

猜猜它沒有被存檔很長時間...不再可用... – KDT 2015-01-22 06:38:43

4

可以添加地圖覆蓋擴展方法關閉IMappingEngine,需要一個參數數組鏈接。喜歡的東西:

public static class AutoMapperExtensions 
{ 
    public static T Map<T>(this IMappingEngine engine, params object[] sources) where T : class 
    { 
     if (sources == null || sources.Length == 0) 
      return default(T); 

     var destinationType = typeof (T); 
     var result = engine.Map(sources[0], sources[0].GetType(), destinationType) as T; 
     for (int i = 1; i < sources.Length; i++) 
     { 
      engine.Map(sources[i], result, sources[i].GetType(), destinationType); 
     } 

     return result; 
    } 
} 

然後,您可以這樣調用它:

var result = Mapper.Engine.Map<MyViewModel>(dto1, dto2, dto3); 
7

如果你有2 DTO類和1扁平視圖模型:

public class Dto1 
{ 
    public string Property1 { get; set; } 
} 
public class Dto2 
{ 
    public string Property2 { get; set; } 
} 
public class FlattenedViewModel 
{ 
    public string Property1 { get; set; } 
    public string Property2 { get; set; } 
} 

與您共創兩個映射DTOs查看模式:

CreateMap<Dto1, FlattenedViewModel>(); 
CreateMap<Dto2, FlattenedViewModel>(); 

您可以第一個DTO映射到模型,然後就「追加」第二DTO:

var dto1 = new Dto1 { Property1 = "Value1"; } 
var dto2 = new Dto2 { Property2 = "Value2"; } 

var model = Mapper.Map<FlattenedViewModel>(dto1); // map dto1 properties 
Mapper.Map(dto2, model); // append dto2 properties 
0

我只是工作這一點我自己,有一個很好的解決方案。這很可能是您的兩個視圖實際上在您的系統中以某種方式相關(特別是如果您使用的是實體框架)。檢查你的模型,你應該看到顯示關係的東西,如果你沒有,那麼只需添加它。 (該virtual

你的模型

public class Dto1 
    { 
     public int id { get; set; } 
     public string Property2 { get; set; } 
     public string Property3 { get; set; } 
     public string Property4 { get; set; } 
     public string Property5 { get; set; } 

     public virtual Dto2 dto2{ get; set; } 

    } 

    public class Dto2 
    { 
     public int id { get; set; } 
     public string PropertyB { get; set; } 
     public string PropertyC { get; set; } 
     public string PropertyD { get; set; } 
     public string PropertyE { get; set; } 
    } 

你的ViewModels

public class Dto1ViewModel 
    { 
     public string Property1 { get; set; } 
     public string Property2 { get; set; } 

     public virtual Dto2VMForDto1 dto2{ get; set; } 
    } 

//Special ViewModel just for sliding into the above 
    public class Dto2VMForDto1 
    { 
     public int id { get; set; } 
     public string PropertyB { get; set; } 
     public string PropertyC { get; set; } 
    } 

Automapper看起來是這樣的:

 cfg.CreateMap< Dto1, Dto1ViewModel>(); 
     cfg.CreateMap< Dto2, Dto2VMForDto1 >(); 

我假設你正在使用LINQ讓你的數據:

Dto1ViewModel thePageVM = (from entry in context.Dto1 where...).ProjectTo<Dto1ViewModel>(); 

中提琴,一切都會奏效。在您看來,只需通過使用model.dto2.PropertyB

相關問題