2017-12-28 12 views
0

AutoMapper를 사용할 때 몇 가지 문제가 있습니다. 내가 할 수있는 모든 곳을 수색했지만 솔루션을 이해하지 못하거나 (코드가 제공되지 않을 때) 또는 내 상황에 적용되지 않는다.프로필의 매개 변수로 데이터 집합이있는 AutoMapper

내 프로필이 있습니다

public class MyCustomProfile : Profile 
{ 
    public MyCustomProfile() 
    { 
     CreateMap<DTO.Person, MyDataSet.PersonRow>(); 
    } 
} 

을 그리고 나는 내 방법이 있습니다

public static void TestAutoMapper(DTO.Person p) 
{ 
    if (Mapper.Instance == null) 
    { 
     Mapper.Initialize(cfg => cfg.AddProfile<MyCustomProfile>()); 
    } 

    MyDataSet.PersonRow pr = Mapper.Map<MyDataSet.PersonRow>(p); 
} 

을하지만 내 문제는 : 나는 dtsMyDataSet의 인스턴스 내 프로필 .ConstructUsing(p => dts.get_XYdataset().Person.NewPersonRow());에 추가 할 필요가 .

dts.get_XYdataset().Person.AddPersonRow(pr) 

그러나 나는 무엇을 해야할지하지 않습니다

는 그리고 나는 또한 다음과 같은 결과 MyDataSet.PersonRow pr을 저장하는 방법 TestAutoMapper(DTO.Person p)MyDataSet dts의 인스턴스가 필요합니다. TestAutoMapper() 메서드에 모든 것을 넣으면 잘 작동하지만 물론 깨끗하지는 않으며 매퍼를 초기화 할 때 프로필을 만들고 호출하여 논리를 구분하고 싶습니다.

public static void TestAutoMapper(DTO.Person p) 
{ 
    if (Mapper.Instance == null) 
    { 
     Mapper.Initialize(cfg => cfg.AddProfile<MyCustomProfile>()); 
    } 

    using (MyDataSet dts = new MyDataSet()) 
    { 
     MyDataSet.PersonRow pr = Mapper.Map<MyDataset.PersonRow>(p, opt => opt.Items["Dataset"] = dts); 
     dts.get_XYdataset().Person.AddPersonRow(pr); 
    } 

가 그럼 난 정의 리졸버에 대한 미니 TUTO을 따르도록 노력하고이 클래스의 구현 : 편집

그래서 나는이 같은 내 TestAutoMapper() 방법이 수정

public class CustomResolver: IMemberValueResolver<object, object, MyDataSet, MyDataSet> 
{ 
    public MyDataSet Resolve(object source, object destination, MyDataSet dts, MyDataSet dts2, ResolutionContext context) 
    { 
     if (dts != null) 
     { 
      return dts; 
     } 
     else if (dts2 != null) 
     { 
      return dts2; 
     } 
     return new MyDataSet(); 
    } 
} 

을 그러나 나는 이것이 괜찮다고 생각하지 않는다. 그러나 잘, 나는 어떻게해서든지 시험해 보았다. 하지만 지금은 내 프로필 생성자 CreateMap<DTO.Person, MyDataSet.PersonRow>() 성명에 갇혀있어. .ConstructUsing()Options.Items["Dataset"]을 얻는 방법?

예제는 멤버에 대한 CustomResolver를 보여 주지만 생성자를 지정하는 방법은 무엇입니까? 심지어 문서를 읽어
CreateMap<DTO.Person, MyDataSet.PersonRow>() .ConstructUsing(Options.Items["Dataset"].get_XYdataset().Person.NewPersonRow());

내가 바보가 될 수 도움을 내 필요를 알고 있지만 난 정말 이해가 안 : 내가 좋아하는 뭔가를 할 수 있다면
그것은 너무 완벽하다.

당신은 어떻게 생각하십니까?

public static class PersonManager 
{ 
    private static MapperConfiguration _config; 
    private static IMapper _mapper; 

    public static void TestAutoMapper(DTO.Person p) 
    { 
     if (_config == null) 
     { 
      _config = new MapperConfiguration(cfg => cfg.AddProfile<MyCustomProfile>()); 
     } 

     if (_mapper == null) 
     { 
      _mapper = _config.CreateMapper(); 
     } 

     using (MyDataSet dts = new MyDataSet()) 
     { 
      XYdataset.PersonRow pr = _mapper.Map<XYdataset.PersonRow>(p, opt => opt.Items["Dataset"] = dts); 
      dts.get_XYdataset().Person.AddPersonRow(pr); 
     } 
    } 
} 

을 그리고 난 내 프로필이 :

감사합니다,

Hellcat8 좋아

+0

HTTP : //docs.automapper합니다. org/ko/stable/Custom-value-resolvers.html # 패스 인 키 값에서 매퍼 –

+0

감사합니다. 이미 읽었습니다. 하지만 .. 어떻게? 나는 내 상황에 어떻게 적용 할 수 있는지 이해할 수 없다. 어쩌면 내가 뭔가를 놓치고 있거나 그냥 공휴일을 필요로 할지도 모르지만 그 일을 정말로 이해하지는 못합니다. – Hellcat8

+0

몇 가지 조사 만하면됩니다.AM repo의 테스트도 유용 할 수 있습니다. –

답변

0

는 결국이 코드와 함께 작동

public class MyCustomProfile : Profile 
{ 
    public MyCustomProfile() 
    { 
     CreateMap<DTO.Person, XYdataset.PersonRow>() 
      .ConstructUsing((source, resolutionContext) => 
      (resolutionContext.Items["Dataset"] as MyDataSet) 
      .(get_XYdataset().Person.NewPersonRow()); 
    } 
} 
관련 문제