c# automapper - 纬度、经度到点转换器

c# automapper - Latitude, Longitude to Point converter

我正在尝试将 ViewModel 映射到模型。我是 Automapper 的新手,我发现文档不完整。经过几个小时的搜索,Whosebug 似乎是最后一站。

这就是我要映射的内容:
ViewModel

public class UpdatedCompleteLocation
    {
        public double? Longitude { get; set; }
        public double? Latitude { get; set; }
        public string? Address { get; set; }
        public string? City { get; set; }
        public string? CountryCode { get; set; }
        public string? Region { get; set; }
        public string? Country { get; set; }
    }

收件人:

模特

public class EventLocation
    {        
        public int Id { get; set; }

        public string? EntityName { get; set; }

        public string? City { get; set; }

        public string? Region { get; set; }

        public string? Address { get; set; }

        public string? Country { get; set; }

        public string? CountryCode { get; set; }

        [Column(TypeName = "geometry (point)")]
        public Point Location { get; set; }
    }

当我尝试将经度、纬度映射到属于 NetTopologySuite.Geometries 的点类型 Location 中的 X、Y 时,技巧就来了。同样作为先决条件,lat/long 必须不为空。

这是我到目前为止想出的:

 CreateMap<UpdatedCompleteLocation, EventLocation>()
             .ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));

如果您想坚持使用 Automapper,则必须为 Location 属性 添加自定义转换。使用现有的 CreateMap,您可以像这样扩展它:

CreateMap<UpdatedCompleteLocation, EventLocation>()
   .ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null))
   .ForMember(dest => dest.Location, opt => opt.MapFrom(src => new Point(src.Longitude, src.Latitude));