Reputation: 33
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; }
}
Auto Mapper
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);
}
My source class is Restaurant and Destination class is RestaurantInfo. Auto mapper is converting Restaurant back into RestaurantInfo, but issue is Address property of RestaurantInfo is not getting initialized with all address related properties of Restaurant. I think my mapping code is not correct. Suggest me correct mapping for above issue.
Upvotes: 1
Views: 2542
Reputation: 2121
You can archive that by using ForPath method
// 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);
Also please referer to the Automapper documentation.
Upvotes: 1
Reputation: 1541
Automapper will flatten an object by convention - https://stackoverflow.com/a/8259466/5121114. Meaning that if you where mapping from RestaurantInfo to Restaurant you could prefix the properties that relate to the Address object with "Address" and automapper would figure out the mapping for you. However what you want to do is unflatten the object and construct an Address object and that is not a feature out of the box. You can possibly write an extension method to achieve this as described in the following post: http://martinburrows.net/blog/2016/01/18/automatic-unflattening-with-automapper
However I'd prefer to be explicit with my mapping:
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
}));
Upvotes: 0