Solved

Using AutoMapper with flattened, nested classes

Posted on 2016-09-10
4
45 Views
Last Modified: 2016-09-16
I am using AutoMapper 4.2 in my .net project and am having problems having flattened properties map from the ViewModel back to the View.   The property maps fine from View to ViewModel but not the other way around.

In the following example, Customer is configured as nested property of Order.  The associated ViewModel flattens Customer.Name to the CustomerName string property.

    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal CreditLimit { get; set; }
        public decimal RemainingCredit { get; set; }
    }

    public class Order
    {
        public int Id { get; set; }
        public Customer Customer { get; set; }
        public int TotalQty { get; set; }
        public decimal TotalValue { get; set; }
        public DateTime ShipDate { get; set; }

        public Order()
        {
            Customer = new Customer();
        }
    }

    public class OrderViewModel
    {
        public int Id { get; set; }
        public string CustomerName { get; set; }
        public int TotalQty { get; set; }
    }

Open in new window


The below "Test Model => ViewModel" runs great with orderViewModel1.CustomerName set to "Abe Anton".  However the "Test ViewModel => Model" section does not work, with order2.Customer.Name set to Null.

            //set up automapper
            var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<OrderViewModel, Order>();
                cfg.CreateMap<Order, OrderViewModel>();
            });
            var mapper = config.CreateMapper();

            //Test Model => ViewModel 
            var order1 = new Order();
            order1.Customer.Name = "Abe Anton";
            order1.TotalQty = 1;
            var orderViewModel1 = mapper.Map<OrderViewModel>(order1);

            //Test ViewModel => Model 
            var orderViewModel2 = new OrderViewModel();
            orderViewModel2.CustomerName = "Betty Basin";
            orderViewModel2.TotalQty = 2;
            var order2 = mapper.Map<Order>(orderViewModel2);

Open in new window

0
Comment
Question by:canuckconsulting
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
Prakash Samariya earned 500 total points
Comment Utility
You can try with custom mapping with auto-mapper
 var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<OrderViewModel, Order>();
                cfg.CreateMap<Order, OrderViewModel>().ForMember(o => o.Name , b => b.MapFrom(z => z.CustomerName ));
            });

Open in new window



https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers
0
 
LVL 8

Expert Comment

by:Prakash Samariya
Comment Utility
@canuckconsulting, didn't you get your answer?

Hope you might got resolution from my answer!
0
 

Author Closing Comment

by:canuckconsulting
Comment Utility
Thansk Prakash and sorry for the late reply.
1
 
LVL 8

Expert Comment

by:Prakash Samariya
Comment Utility
Your welcome :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now