Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper mapping when property doesn't exist in source

Tags:

c#

automapper

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?

like image 672
jgill09 Avatar asked Jun 10 '26 08:06

jgill09


1 Answers

.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})));
like image 163
Larry Dukek Avatar answered Jun 12 '26 22:06

Larry Dukek