0

私は AutoMapper に慣れてきたばかりで、その仕組みが気に入っています。ただし、現在手動​​で配線しているいくつかの複雑なシナリオをマップできると思います。以下の単純化された例から手動プロセスを削除し、学習曲線を加速するための提案/ヒントはありますか?

次のようなソース オブジェクトがあります。

public class Source
{
    public Dictionary<string, object> Attributes;
    public ComplexObject ObjectWeWantAsJson;
}

そして、次のような宛先オブジェクト:

public class Destination
{
    public string Property1; // Matches with Source.Attributes key
    public string Property2; // Matches with Source.Attributes key
    // etc.
    public string Json;
}

AutoMapper の構成は最小限です。

var config = new MapperConfiguration(cfg => {});
var mapper = config.CreateMapper();

そして、私の変換コードは次のようになります:

var destinationList = new List<Destination>();
foreach (var source in sourceList)
{
    var dest = mapper.Map<Dictionary<string, object>, Destination(source.Attributes);
    // I'm pretty sure I should be able to combine this with the mapping
    // done in line above
    dest.Json = JsonConvert.SerializeObject(source.ObjectWeWantAsJson);

    // I also get the impression I should be able to map a whole collection
    // rather than iterating through each item myself
    destinationList.Add(dest);
}

ポインタや提案は大歓迎です。前もって感謝します!

4

1 に答える 1