Techiehook Techiehook
Updated date May 09, 2024
In this article, we will learn how to easily map nested objects in C# using AutoMapper including sample code and output.

How to Map Nested Objects in C# with AutoMapper?

To map nested objects using AutoMapper in C#, we first need to set up the mapping configuration between your source and destination classes. Then you can use AutoMapper's Mapper.Map method to perform the mapping. The below program shows how to map the nested objects,

using AutoMapper;
using System;

public class Employee1
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public Address1 Address { get; set; }
    public Contact1 Contact { get; set; }
}

public class Address1
{
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

public class Contact1
{
    public string Name { get; set; }
    public string Relationship { get; set; }
}

public class Employee2
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public Address2 Address { get; set; }
    public Contact2 Contact { get; set; }
}

public class Address2
{
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

public class Contact2
{
    public string Name { get; set; }
    public string Relationship { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // Setup AutoMapper configuration
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Employee1, Employee2>();
            cfg.CreateMap<Address1, Address2>();
            cfg.CreateMap<Contact1, Contact2>();
        });

        // Create mapper instance
        IMapper mapper = config.CreateMapper();

        // Create an instance of Employee1 with the provided data
        var employee1 = new Employee1
        {
            Id = 1,
            Name = "Smith",
            Position = "Engineer",
            Address = new Address1
            {
                Line1 = "2000 Main St",
                Line2 = "Apt 250",
                City = "Bangalore",
                State = "Karnataka"
            },
            Contact = new Contact1
            {
                Name = "Raj",
                Relationship = "Friend"
            }
        };

        // Map Employee1 to Employee2
        var employee2 = mapper.Map<Employee2>(employee1);

        // Display the mapped/updated result
        Console.WriteLine("Employee2:");
        Console.WriteLine($"Id: {employee2.Id}");
        Console.WriteLine($"Name: {employee2.Name}");
        Console.WriteLine($"Position: {employee2.Position}");
        Console.WriteLine($"Address:");
        Console.WriteLine($"  Line1: {employee2.Address.Line1}");
        Console.WriteLine($"  Line2: {employee2.Address.Line2}");
        Console.WriteLine($"  City: {employee2.Address.City}");
        Console.WriteLine($"  State: {employee2.Address.State}");
        Console.WriteLine($"Contact:");
        Console.WriteLine($"  Name: {employee2.Contact.Name}");
        Console.WriteLine($"  Relationship: {employee2.Contact.Relationship}");

        Console.ReadLine();
    }
}

The above code sets up AutoMapper to map between Employee1 and Employee2, Address1 and Address2, and Contact1 and Contact2. Then it creates an instance of Employee1, executes the mapping, and displays the result (see below).

Output:

Employee2:
Id: 1
Name: Smith
Position: Engineer
Address:
  Line1: 2000 Main St
  Line2: Apt 250
  City: Bangalore
  State: Karnataka
Contact:
  Name: Raj
  Relationship: Friend

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!