当我的 class 中有 属性 时,我该如何映射,它具有提供值自动映射器的默认构造函数?
How can I map when I've property in my class which have default constructor to provide values automapper?
我想创建一个从 UserDto 到 User 实体的映射,请帮助我如何实现它。我在用户实体中有 GeoLocation 属性 如何映射这些属性。有人可以举例说明解决方案吗?
我正在使用 AutoMapper 包:https://www.nuget.org/packages/AutoMapper/
我的用户实体class:
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public GeoLocation PurchaseLocationCoordinates { get; set; }
}
我的 Dto class:
public class UserDto
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
public string PurchaseLocationLatitude { get; set; }
public string PurchaseLocationLongitude { get; set; }
}
地理位置class:
public class GeoLocation
{
public GeoLocation(double lon, double lat)
{
Type = "Point";
if (lat > 90 || lat < -90) { throw new ArgumentException("A latitude coordinate must be a value between -90.0 and +90.0 degrees."); }
if (lon > 180 || lon < -180) { throw new ArgumentException("A longitude coordinate must be a value between -180.0 and +180.0 degrees."); }
Coordinates = new double[2] { lon, lat };
}
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("coordinates")]
public double[] Coordinates { get; set; }
public double? Lat() => Coordinates?[1];
public double? Lon() => Coordinates?[0];
}
映射:
CreateMap<UserDto, User>();
可以参考这段代码:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<UserDto, User>()
.ForMember(x => x.PurchaseLocationCoordinates, opt => opt.MapFrom(model => model));
cfg.CreateMap<UserDto, GeoLocation>()
.ForCtorParam("lon", opt => opt.MapFrom(src => src.PurchaseLocationLongitude))
.ForCtorParam("lat", opt => opt.MapFrom(src => src.PurchaseLocationLatitude));
});
UserDto userdto = new UserDto()
{
PurchaseLocationLongitude = "80.44",
PurchaseLocationLatitude = "34.56"
};
IMapper iMapper = config.CreateMapper();
var user = iMapper.Map<UserDto, User>(userdto);
我想创建一个从 UserDto 到 User 实体的映射,请帮助我如何实现它。我在用户实体中有 GeoLocation 属性 如何映射这些属性。有人可以举例说明解决方案吗?
我正在使用 AutoMapper 包:https://www.nuget.org/packages/AutoMapper/
我的用户实体class:
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public GeoLocation PurchaseLocationCoordinates { get; set; }
}
我的 Dto class:
public class UserDto
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
public string PurchaseLocationLatitude { get; set; }
public string PurchaseLocationLongitude { get; set; }
}
地理位置class:
public class GeoLocation
{
public GeoLocation(double lon, double lat)
{
Type = "Point";
if (lat > 90 || lat < -90) { throw new ArgumentException("A latitude coordinate must be a value between -90.0 and +90.0 degrees."); }
if (lon > 180 || lon < -180) { throw new ArgumentException("A longitude coordinate must be a value between -180.0 and +180.0 degrees."); }
Coordinates = new double[2] { lon, lat };
}
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("coordinates")]
public double[] Coordinates { get; set; }
public double? Lat() => Coordinates?[1];
public double? Lon() => Coordinates?[0];
}
映射:
CreateMap<UserDto, User>();
可以参考这段代码:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<UserDto, User>()
.ForMember(x => x.PurchaseLocationCoordinates, opt => opt.MapFrom(model => model));
cfg.CreateMap<UserDto, GeoLocation>()
.ForCtorParam("lon", opt => opt.MapFrom(src => src.PurchaseLocationLongitude))
.ForCtorParam("lat", opt => opt.MapFrom(src => src.PurchaseLocationLatitude));
});
UserDto userdto = new UserDto()
{
PurchaseLocationLongitude = "80.44",
PurchaseLocationLatitude = "34.56"
};
IMapper iMapper = config.CreateMapper();
var user = iMapper.Map<UserDto, User>(userdto);