2014-02-19 72 views
0

保持這個基本我有兩種模式。客戶和CustomerViewModel。我想映射一個到另一個之間的屬性。地圖IEnumerable模型查看

public class Customer 
{ 
    public string CompanyName { get; set; } 
    public int CustomerType { get; set; } 
    public IEnumerable<ContactViewModel> CustomerContacts { get; set; } 
} 

public class CustomerViewModel 
{ 
    public string CompanyName { get; set; } 
    public int CustomerType { get; set; } 
    public ContactName {get;set;} 
    public ContactTel {get;set;} 
} 

問題是我想將customerContact映射到contactName和contactTel。我知道它是一個IEnumerable幷包含聯繫人列表,但在此過濾器中,IEnumerable的聯繫人視圖模型將只包含1條記錄。

我該如何在automapper中做這種類型的映射?

UPDATE:

我決定去與前面提到的類似approuch人。我希望能在automapper中做到這一點,但我想它稍微複雜一些。

public static CustomerListViewModel MapToListView(this CustomerServiceModel svcModel) 
    { 
     ContactServiceModel contact = new ContactServiceModel { Name = String.Empty, Telephone = String.Empty }; 
     if(svcModel.CustomerContacts != null) contact = svcModel.CustomerContacts.FirstOrDefault(); 

     return new CustomerListViewModel 
     { 
      Id = svcModel.Id, 
      address = svcModel.Address.MapToView(), 
      CompanyName = svcModel.CompanyName, 
      ContactName = contact.Name, 
      ContactTelephone = contact.Telephone 
     }; 
    } 
+0

你想在映射期間過濾,還是您知道它在映射之前只有一條記錄? – Maess

+0

我知道它只會保存一個記錄。 –

+0

@JamesAndrewSmith,Linq似乎是要走的路,您是否必須使用automapper? – Yohannes

回答

1

我不確定這可以用AutoMapper完成。你可以嘗試定義一個擴展方法,例如:

public static CustomerViewModel ToViewModel(this Customer cust) 
     { 
      return new CustomerViewModel() 
       { 
        CompanyName = cust.CompanyName, 
        CustomerType = cust.CustomerType, 
        ContactName = cust.CustomerContacts.First().ContactName, 
        ContactTel = cust.CustomerContacts.First().ContactTel 
       }; 
     } 

當然,在頂部稍微驗證會很好。 然後你使用這樣的:

Customer cust= GetCustomer(); 
CustomerViewModel model= cust.ToViewModel(); 
0

嘗試使用LINQ

public ActionResult Index() 
{ 
    var viewmodel = CustomerViewModel(); 

    return View(viewmodel); 
} 

private static CustomerViewModel() 
{ 
    return new CustomerViewModel 
    { 
    ... 
    ContactName = BusinessLogic.GetContactName("Microsoft"); 
    ContactTel = BusinessLogic.GetContactTel(); 
    } 
} 

哪裏BusinessLogic是你的 「大腦」 級,其決定如何從數據中獲得,並有使用LINQ獲得電話和姓名:

public static class BusinessLogic 
{ 
    public static string GetContactName(string companyName) 
    { 
     var c = new Customer() ... // get you Customer-object 

     var q = (from contacts in c.CustomerContacts 
       where contacts.CompanyName == companyName 
       select contacts.ContactName).First(); 

     return q; 
    } 
} 

但是我可以看到一個錯誤。您將視圖模型數據(您想要在網頁上爲用戶展示)和數據模型(其呈現對象模型「聯繫人」)混淆。換句話說,最好使用特殊模型類作爲您的CustomerContacts的類型。對不起,我的「好」英語。希望我已經幫助你。

0

這將拋出一個異常,如果CustomerContactsnull或空(我會盡量拿出一個更強大的解決方案),但這裏的東西會爲快樂工作路徑:

Mapper.CreateMap<Customer, CustomerViewModel>() 
    .ForMember(dest => dest.ContactName, opt => opt.MapFrom(src => src.CustomerContacts.First().ContactName)) 
    .ForMember(dest => dest.ContactTel, opt => opt.MapFrom(src => src.CustomerContacts.First().ContactTel)); 

也許是更安全的選擇將是把邏輯的Customer類本身的內部:

public class Customer 
{ 
    /* other members */ 
    public string ContactName 
    { 
     get 
     { 
      string name = null; 
      if (CustomerContacts != null && CustomerContacts.Any()) 
      { 
       name = CustomerContacts.First().ContactName; 
      } 

      return name; 
     } 
    } 

    public string ContactTel 
    { 
     get 
     { 
      string tel = null; 


      if (CustomerContacts != null && CustomerContacts.Any()) 
      { 
       tel = CustomerContacts.First().ContactTel; 
      } 
      return tel; 
     } 
    } 

}