Solved

Using AutoMapper with flattened, nested classes

Posted on 2016-09-10
4
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
Prakash Samariya earned 500 total points
ID: 41794012
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 10

Expert Comment

by:Prakash Samariya
ID: 41801240
@canuckconsulting, didn't you get your answer?

Hope you might got resolution from my answer!
0
 

Author Closing Comment

by:canuckconsulting
ID: 41801244
Thansk Prakash and sorry for the late reply.
1
 
LVL 10

Expert Comment

by:Prakash Samariya
ID: 41801245
Your welcome :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

695 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