2017-12-28 27 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());に追加する必要があり 。

そして私はまた、次のように、結果MyDataSet.PersonRow prを保存する方法TestAutoMapper(DTO.Person p)MyDataSet dtsのこのインスタンスが必要になります。

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

しかし、私は何をすべきかわかりません。 TestAutoMapper()メソッドにすべてを入れるとうまくいきますが、それはきれいではないですし、プロファイルを作成してマッパーを初期化するときにロジックを分離したいと思っています。

EDIT

だから私はこのように私のTestAutoMapper()方法変更:その後、私はカスタムリゾルバについてのミニtutoを追跡しようとしたと、このクラスを実装

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

を:

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());

は、私は愚かなことができ、ヘルプのための私の必要性を知っているが、私は本当に理解していない:私はのような何かを行うことができれば
はそれはとても完璧になります。

あなたはどう思いますか?

おかげで、

Hellcat8

+0

ます。http://docs.automapperを。 org/ja/stable/Custom-value-resolvers.html#キー入力時のキー値へのマッチング –

+0

ありがとう、私はすでに読んでいますそれはどうですか?私は自分の状況にどうやって適用するのか分かりません。たぶん私は何かを見逃している、または単に休日が必要ですが、私は事を本当に理解していません。 – Hellcat8

+0

あなたはちょっとした研究をするだけです。AMレポのテストも便利です。 –

答えて

0

オーケー最終的には、このコードで動作します:

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

そして、私は私のプロフィールがあります

public class MyCustomProfile : Profile 
{ 
    public MyCustomProfile() 
    { 
     CreateMap<DTO.Person, XYdataset.PersonRow>() 
      .ConstructUsing((source, resolutionContext) => 
      (resolutionContext.Items["Dataset"] as MyDataSet) 
      .(get_XYdataset().Person.NewPersonRow()); 
    } 
} 
関連する問題