次のエンティティモデルがあるとします。
public class Location
{
public int Id { get; set; }
public Coordinates Center { get; set; }
}
public class Coordinates
{
public double? Latitude { get; set; }
public double? Longitude { get; set; }
}
...および次のビューモデル:
public class LocationModel
{
public int Id { get; set; }
public double? CenterLatitude { get; set; }
public double? CenterLongitude { get; set; }
}
LocationModelプロパティは、エンティティからモデルへのマッピングがカスタムリゾルバーを必要としないように名前が付けられています。
ただし、モデルからエンティティにマッピングする場合は、次のカスタムリゾルバーが必要です。
CreateMap<LocationModel, Location>()
.ForMember(target => target.Center, opt => opt
.ResolveUsing(source => new Coordinates
{
Latitude = source.CenterLatitude,
Longitude = source.CenterLongitude
}))
どうしてこれなの?AutoMapperを作成して、ビューモデルの命名規則に基づいて新しいCoordinates値オブジェクトを作成する簡単な方法はありますか?
アップデート
最初のコメントに答えるために、ビューモデルマッピングへのエンティティについて特別なことは何もありません。
CreateMap<Location, LocationModel>();