Mapping Complex type to Primitive Type using AutoMapper in C#

Mapping Complex type to Primitive Type using AutoMapper in C#

In this article, I am going to discuss how to map Complex type to Primitive Type using AutoMapper in C# with examples. When one class contains primitive types or you can say the simple types and the other class contains the complex type involved in the mapping then in such scenarios we need to Map the Complex Type to the Primitive Types. Please read our previous article before proceeding to this article where we discussed the Automapper Complex Mapping in C# with some examples.

Let us understand how to map Complex type to Primitive Type using AutoMapper in C# with an example. Here in this demo, we are going to use the following three classes.

how to map Complex type to Primitive Type using AutoMapper in C#

As shown in the above image, we want to Map the complex property address of Employee type to the City, State and Country properties of EmployeeDTO type.

To do this we need initializes the Mapper configuration as shown below.

how to map Complex type to Primitive Type using AutoMapper in C#

As shown in the above image we mapped each property from the complex type (address) of the source object (Employee) to the correspondent properties of the destination object (EmployeeDTO).

Below is the complete code of the above example.
using AutoMapper;
namespace AutoMapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step1: Call the InitializeAutomapper method to initialize the Mapper
            //In InitializeAutomapper method we defined the Mappings
            InitializeAutomapper();

            //Step2: Create and populate the Employee object
            Address empAddres = new Address()
            {
                City = "Mumbai",
                State = "Maharashtra",
                Country = "India"
            };

            Employee emp = new Employee();
            emp.Name = "James";
            emp.Salary = 20000;
            emp.Department = "IT";
            emp.address = empAddres;
            
            //Step3: Use the mapper to map the employee data with the Employee DTO
            var empDTO = Mapper.Map<Employee, EmployeeDTO>(emp);
            
            Console.WriteLine("Name:" + empDTO.Name + ", Salary:" + empDTO.Salary +", Department:" + empDTO.Department);
            Console.WriteLine("City:" + empDTO.City + ", State:" + empDTO.State + ", Country:" + empDTO.Country);
            Console.ReadLine();
        }

        static void InitializeAutomapper()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Employee, EmployeeDTO>()
                .ForMember(dest => dest.City, act => act.MapFrom(src => src.address.City))
                .ForMember(dest => dest.State, act => act.MapFrom(src => src.address.State))
                .ForMember(dest => dest.Country, act => act.MapFrom(src => src.address.Country));
            });
        }
    }

    public class Employee
    {
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Department { get; set; }
        public Address address { get; set; }
    }

    public class EmployeeDTO
    {
        public string Name { get; set; }
        public int Salary { get; set; }            
        public string Department { get; set; }     
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }
    
    public class Address
    {
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }
}

Now run the application and you will see the output as expected as shown below.

Mapping Complex type to Primitive Type using AutoMapper

Let’s modify the requirement. Now we want to map the primitive type properties to a complex type as shown in the image below.

Mapping Complex type to Primitive Type using AutoMapper

As you can see in the above image, now we want to map the City, State, and Country Properties of the Employee object to the address property of the EmployeeDTO object. To achieve this we need to use projection in AutoMapper as shown below.

Mapping Complex type to Primitive Type using AutoMapper

As you can see in the above image, we are projection an object (Address type) using the MapForm option and City, State and Country Values are coming from the Source object.

Below is the complete example.
Below is the complete example.
using AutoMapper;
namespace AutoMapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step1: Call the InitializeAutomapper method to initialize the Mapper
            //In InitializeAutomapper method we defined the Mappings
            InitializeAutomapper();

            //Step2: Create and populate the Employee object
            Address empAddres = new Address()
            {
                City = "Mumbai",
                State = "Maharashtra",
                Country = "India"
            };

            Employee emp = new Employee();
            emp.Name = "James";
            emp.Salary = 20000;
            emp.Department = "IT";
            emp.City = "Mumbai";
            emp.State = "Maharashtra";
            emp.Country = "India";
            
            //Step3: Use the mapper to map the employee data with the Employee DTO
            var empDTO = Mapper.Map<Employee, EmployeeDTO>(emp);
            
            Console.WriteLine("Name:" + empDTO.Name + ", Salary:" + empDTO.Salary +", Department:" + empDTO.Department);
            Console.WriteLine("City:" + empDTO.address.City + ", State:" + empDTO.address.State + ", Country:" + empDTO.address.Country);
            Console.ReadLine();
        }

        static void InitializeAutomapper()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Employee, EmployeeDTO>()
                .ForMember(dest => dest.address, act => act.MapFrom(src => new Address()
                {
                    City = src.City,
                    State = src.State,
                    Country = src.Country
                }));
            });
        }
    }

    public class Employee
    {
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Department { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }

    public class EmployeeDTO
    {
        public string Name { get; set; }
        public int Salary { get; set; }            
        public string Department { get; set; }   
        public Address address { get; set; }
    }
    
    public class Address
    {
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }
}

When you run the application, it will display the output as expected as shown below.

Mapping Complex type to Primitive Type using AutoMapper

In the next article, I am going to discuss Reverse Mapping using AutoMapper in C# with examples.

SUMMARY:

In this article, I try to explain how to map Complex type to Primitive Type using AutoMapper in C# with some examples. I hope this  Complex type to Primitive Type using AutoMapper in C# article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

1 thought on “Mapping Complex type to Primitive Type using AutoMapper in C#”

Leave a Reply

Your email address will not be published. Required fields are marked *