1
votes

I'm trying to map a ViewModel to a Domain class using Automapper.

However, the ViewModel contains a property that doesn't exist in the Domain class.

Instead, I am using a different property, Product, from the Domain class to map to the ViewModel Price property but this doesn't appear to be working.

public class OrderViewModel
{
    public IEnumerable<Product> Product { get; set; }

    public IEnumerable<Price> Price { get; set; }

}

public class Order
{
    public IEnumerable<Product> Product { get; set; }
}

public class Product
{
    public string Code { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public decimal Amount { get; set; }

    public decimal DeliveryCharge { get; set; }

}

public class Price
{
    public string UniqueProductCode { get; set; }

    public decimal Charge { get; set; }

    public decimal CourierCharge { get; set; }

}

Then in my Mapping Profile I have setup the following:

public class DomainToViewModelMappingProfile : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "DomainToViewModelMappings";
            }
        }

        public DomainToViewModelMappingProfile()
        {
            ConfigureMappings();
        }


        /// <summary>
        /// Creates a mapping between source (Domain) and destination (ViewModel)
        /// </summary>
        private void ConfigureMappings()
        {

           CreateMap<Core.Domain.Product, Web.Models.Price>()
               .ForMember(dest => dest.UniqueProductCode,
                          opts => opts.MapFrom(src => src.Code))
               .ForMember(dest => dest.Charge,
                          opts => opts.MapFrom(src => src.Amount))
               .ForMember(dest => dest.CourierCharge,
                          opts => opts.MapFrom(src => src.DeliveryCharge));

    CreateMap<Core.Domain.Order, OrderViewModel>()
                    .ForMember(dest => dest.Product, 
                               opts => opts.MapFrom(src => src.Product))
                    .ForMember(dest => dest.Price,
                               opts => opts.MapFrom(src => new List<Price>() ));
        }
    }

Then in my Controller I have:

var list = _productRepository.GetProducts();
Mapper.Map<Order, OrderViewModel>(list );

But the Price property is empty (not null) as the property has been instantiated. Just there's no records.

What am I doing wrong?

1
can you please show the code that you are using to map? - sachin
.ForMember(dest => dest.Price, opts => opts.MapFrom(src => new List<Price>())); equates to "For the member 'Price', just create an empty list." - is that what you're referring to? - stuartd
Added in the Mapping Profile code @sachin. - jgill09
@stuartd yes, but I am not expecting an empty list. I am expecting the Price property to be populated based on the mappings. - jgill09
You are specifically instructing AutoMapper to use an empty list. Your mapping code is incorrect, and there isn't enough information in the question to see what it should be, e.g. what's the type of list? - stuartd

1 Answers

1
votes
.ForMember(dest=>dest.Price, opts=>opts.MapFrom(src=>new List<Price>()));

Means you are setting dest.Price = new List(); Try this:

.ForMember(dest=>dest.Price, opts=>opts.MapFrom(src=>src.Product.Select(p=>new Price {Charge = p.Amount,CourierCharge = p.DeliveryCharge,UniqueProductCode = p.Code})));