Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using AutoMapper with flattened, nested classes

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

810 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