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. Please read our previous article where we discussed the Automapper Complex Mapping with some examples. At the end of this article, you will understand and when and how to map complex type to primitive type using automapper.

When to map Complex type to Primitive Type using AutoMapper in C#?

When one class contains primitive types or you can say the simple types and the other class contains complex type involved in the mapping then in such scenarios we need to Map the Complex Type to the Primitive Types. 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 (Employee, EmployeeDTO and Address).

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

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

How to map Complex type to Primitive Type using AutoMapper in C#?

In order to map the complex type to the primitive types we need to use the ForMember and need to specify the source and target properties. Here, we need to map the city, state, and Country properties of Address object to the City, state, and Country properties of EmployeeDTO class. To do this you 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).

The complete code of the above example.
using System;
using AutoMapper;
namespace AutoMapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            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;

            var mapper =InitializeAutomapper();
            var empDTO = mapper.Map<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 Mapper InitializeAutomapper()
        {

            var config = new MapperConfiguration(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));
            });
            
            var mapper = new Mapper(config);
            return mapper;
        }
    }

    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

Mapping Primitive properties to a Complex type using Automapper:

Now we want to map the primitive type properties of the source object to a complex type of the destination object 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 source (Employee) object to the address property of the destination (EmployeeDTO) object. In order to achieve this you need to use projection in AutoMapper. So, you need to change the mapper configuration as shown below.

Mapping Primitive properties to a Complex type using Automapper in C#

As you can see in the above image, we project 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
using System;
using AutoMapper;
namespace AutoMapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            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";

            var mapper =InitializeAutomapper();
            var empDTO = mapper.Map<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 Mapper InitializeAutomapper()
        {

            var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<Employee, EmployeeDTO>()
                .ForMember(dest => dest.address, act => act.MapFrom(src => new Address()
                {
                    City = src.City,
                    State = src.State,
                    Country = src.Country
                }));
            });
            
            var mapper = new Mapper(config);
            return mapper;
        }
    }

    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. Here, in this article, I try to explain how to map Complex type to Primitive Type using AutoMapper in C# and Vice Versa with 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.

2 thoughts 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 *