2013-07-07 2 views
0

List<KeyValuePair<string,string>>에 2 개의 elemets가 있습니다. 1) 키 = AgregateOn 값 = 양/체중/수량KeyValuePair 요소에서 엔터티로의 AutoMapper

2) 키 = 최대/최소 값 = 십진수 값 (양/체중) 또는 INT (수량)

// 난 인터페이스

public interface IQuantityRestriction 
{ 
    int? MinQuantity { get; set; } 
    int? MaxQuantity { get; set; } 
    decimal? MinAmount { get; set; } 
    decimal? MaxAmount { get; set; } 
    decimal? MinWeight { get; set; } 
    decimal? MaxWeight { get; set; } 
} 

그래서 내가 AutoMapper ? for example <AgregateOn,Quantity> , <Max,5>IQuantityRestrictionConfigurationData(KeyValuePair List)에서지도 할 수있는 방법 속성 MaxQuantity와 IQuantityRestriction에 매핑해야합니다 = 5. 가능합니까?

+0

이 인터페이스에 매핑하려는 것이 명확하지 않습니다. 데이터 샘플을 제공하십시오. 하지만 처음 보면 수동으로 매핑하는 것이 더 쉬워 보입니다. –

+0

예 : var configurationData = new List > { new KeyValuePair <문자열, 문자열> ("AggregateOn", "Quantity"), 새 KeyValuePair ("Max", "5") }; 그래서이 configurationData를 MaxQuantity 속성이 5 인 IQuantityRestriction에 매핑하고 싶습니다. – user681055

답변

2

Automapper 사용법의 이점이있을 것이라고 생각하지 않습니다. 두 번째 항목의 키와 함께 목록의 첫 번째 항목에서 값으로 매핑해야하며 두 번째 항목의 값을 10 진수로 파싱해야한다고 추측 할 수 없습니다. 이 모든 것을 수동으로 만 할 수 있습니다. 예 : Automapper 매핑 구성은 다음과 같습니다.

Mapper.CreateMap<List<KeyValuePair<string, string>>, QuantityRestriction>() 
     .AfterMap((src, qr) => 
     { 
      switch (src[0].Value) 
      { 
       case "Quantity": 
        switch (src[1].Key) 
        { 
         case "Max": 
          qr.MaxQuantity = Int32.Parse(src[1].Value); 
          break; 
         case "Min": 
          qr.MinQuantity = Int32.Parse(src[1].Value); 
          break; 
        } 
        return; 
       // case "Amount" 
       // case "Weight" 
      } 
     }); 

자동 매핑을 사용하지 않고 매핑하는 것보다 나은 것은 아닙니다.