考虑下面的实体模型:为什么在这里需要自定义解析器(AutoMapper)?
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根据viewmodel中的命名约定构造一个新的坐标值对象?
更新
要回答第一个评论,并没有什么特别之处实体视图模型映射:
CreateMap<Location, LocationModel>();
您可以包括从实体模型的映射? – 2012-02-03 17:31:38
我已经包含了从实体到模型的映射。 – danludwig 2012-02-03 19:01:25