Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using AutoMapper with flattened, nested classes

Posted on 2016-09-10
4
Medium Priority
?
94 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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