2014-02-19 4 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;} 
} 

문제는 contactName 및 contactTel에 customerContact를 매핑하려고합니다. 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

당신이 원하는 :

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 클래스 자체의 내부 논리를 넣어하는 것입니다 필터를 사용하거나 매핑 전에 하나의 레코드 만 가지고 있다는 것을 알고 있습니까? – Maess

+0

하나의 레코드 만 보유 할 것입니다. –

+0

@JamesAndrewSmith, Linq가 가야 할 것 같습니다. 자동 어머 사용은 필수입니까? – 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 
       }; 
     } 

물론 상단에있는 작은 유효성 검사가 좋습니다. 그런 다음 당신은 다음과 같이 사용 : CustomerContacts 경우

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

봅니다 LINQ 사용하는

BusinessLogic 데이터에서 얻을 무엇을 결정하는 당신의 "두뇌"- 클래스이며, 전화 및 이름을 얻기 위해 LINQ가 사용
public ActionResult Index() 
{ 
    var viewmodel = CustomerViewModel(); 

    return View(viewmodel); 
} 

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

:

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의 Type으로 특수 모델 클래스를 사용하는 것이 좋습니다. 내 "좋은"영어로 죄송합니다. 내가 너를 도왔기를 바래.

0

이것은 예외가 발생합니다 null 또는 빈 (내가 더 강력한 솔루션을 마련하려고합니다)하지만, 여기에 행복을 위해 일하는 것이 뭔가 경로 :에

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; 
     } 
    } 

} 
관련 문제