Solved

Using AutoMapper with flattened, nested classes

Posted on 2016-09-10
4
51 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
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 8

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 8

Expert Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 33
imap mails 1 22
HttpPostedFile 1 26
i have to take the screenshot of command prompt? how to do this? 1 37
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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