【问题标题】:Automapper map few source object property to destination object and few to its complex child object propertyAutomapper 将很少的源对象属性映射到目标对象,很少映射到其复杂的子对象属性
【发布时间】:2018-06-18 18:31:17
【问题描述】:
public class Restaurant 
{
        public int RestaurantId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Slug { get; set; }
        public bool Active { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }
        public decimal? Lat { get; set; }
        public decimal? Long { get; set; }
}

public class RestaurantInfo 
{
        public int RestaurantId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Slug { get; set; }
        public bool Active { get; set; }
        public Address Address { get; set; }
}

public class Address
{
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }
        public decimal? Lat { get; set; }
        public decimal? Long { get; set; }
}

自动映射器

public class AutoMapperProfile : Profile
    {
        public AutoMapperProfile()
        {
            CreateMap<Restaurant, RestaurantInfo>();

            CreateMap<Restaurant, Address>();
        }
    }

public RestaurantInfo GetRestaurantById(IMapper mapper)
    {

        var restaurant = new Restaurant
            {   
                RestaurantId = 1,
                Name = "Blueline",
                Slug =  "Blueline",
                Active = true,
                Address1 = "XYZ",
                Address2 = "PQR"
            };

        return mapper.Map<Restaurant>(restaurantInfo);    
    }

我的源类是 Restaurant,Destination 类是 RestaurantInfo。自动映射器将 Restaurant 转换回 RestaurantInfo,但问题是 RestaurantInfo 的 Address 属性未使用 Restaurant 的所有地址相关属性进行初始化。我认为我的映射代码不正确。建议我为上述问题正确映射。

【问题讨论】:

标签: c# automapper


【解决方案1】:

您可以使用 ForPath 方法将其归档

// Configure AutoMapper
Mapper.Initialize(
    cfg => cfg.CreateMap<Restaurant, RestaurantInfo>()
    .ForPath(dest => dest.Address.Address1, opt => opt.MapFrom(src => src.Address1))
    .ForPath(dest => dest.Address.Address2, opt => opt.MapFrom(src => src.Address2))
);

// Perform mapping
var restaurantInfo = Mapper.Map<Restaurant, RestaurantInfo>(restaurant);

另请参考 Automapper documentation

【讨论】:

    【解决方案2】:

    Automapper 将按照惯例展平对象 - https://stackoverflow.com/a/8259466/5121114。这意味着如果您从 RestaurantInfo 映射到 Restaurant,您可以在与 Address 对象相关的属性前加上“Address”,并且 automapper 会为您找出映射。但是,您要做的是展开对象并构造一个 Address 对象,这不是开箱即用的功能。您可以编写一个扩展方法来实现这一点,如下面的帖子所述:http://martinburrows.net/blog/2016/01/18/automatic-unflattening-with-automapper

    不过,我更愿意明确我的映射:

    CreateMap<Restaurant, RestaurantInfo>().ForMember(info => info.Address, 
                    expression => expression.MapFrom(restaurant => new Address
                    {
                        Address1 = restaurant.AddressAddress1,
                        Address2 = restaurant.AddressAddress2,
                        City = restaurant.AddressCity,
                        Lat = restaurant.AddressLat,
                        Long = restaurant.AddressLong,
                        ZipCode = restaurant.AddressZipCode
                    }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-22
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 2014-12-03
      • 2017-03-16
      相关资源
      最近更新 更多