2

How to map a single object to a nested object?

The below one is my entity.

class Employee
{
    public string name { get; set; }
    public string city_name { get; set; }
    public string State_name { get; set; }
}

I want to map to

class Employee
{
    public string Name;
    public Location Location;
}

class Location
{
    public string City;

    public string State;
}

Please note that the property names are different. Can anyone help to map these using AutoMapper?

1
  • 1
    You should just check the automapper documentation and create a custom mapping. looks like the automapper documentation sample is pretty similar to your scenario Commented Sep 22, 2021 at 22:13

1 Answer 1

3

Solution 1: ForPath

1.1 Create Profile instance with inheriting from Profile and put the configuration in the constructor.

1.1.1 Using ForPath to map nested property.

public class EmployeeProfile : Profile
{
    public EmployeeProfile()
    {
        CreateMap<Employee, EmployeeDto>()
            .ForPath(dest => dest.Location.City, opt => opt.MapFrom(src => src.city_name))
            .ForPath(dest => dest.Location.State, opt => opt.MapFrom(src => src.State_name));
    }
}

1.2 Adding profile to mapper configuration

public static void Main()
{
    var config = new MapperConfiguration(cfg =>
    {

        cfg.AddProfile<EmployeeProfile>();

        // Note: Demo program to use this configuration rather with EmployeeProfile
        /*
        cfg.CreateMap<Employee, EmployeeDto>()
            .ForPath(dest => dest.Location.City, opt => opt.MapFrom(src => src.city_name))
            .ForPath(dest => dest.Location.State, opt => opt.MapFrom(src => src.State_name));
        */      
    });
    IMapper mapper = config.CreateMapper();
        
    var employee = new Employee{name = "Mark", city_name = "City A", State_name = "State A"};

    var employeeDto = mapper.Map<Employee, EmployeeDto>(employee);
}

Sample Solution 1 on .Net Fiddle


Solution 2: AfterMap

2.1 Create Profile instance with inheriting from Profile and put the configuration in the constructor.

2.1.1 Using AfterMap to perform mapping nested property after map occurs.

public class EmployeeProfile : Profile
{
    public EmployeeProfile()
    {
        CreateMap<Employee, EmployeeDto>()
            .AfterMap((src, dest) => { dest.Location = 
                    new Location {
                        City = src.city_name,
                        State = src.State_name
                    };
                });
    }
}

2.2 Adding profile to mapper configuration

public static void Main()
{
    var config = new MapperConfiguration(cfg =>
    {

        cfg.AddProfile<EmployeeProfile>();

        // Note: Demo program to use this configuration rather with EmployeeProfile
        /*
        cfg.CreateMap<Employee, EmployeeDto>()
            .AfterMap((src, dest) => { dest.Location = 
                    new Location {
                        City = src.city_name,
                        State = src.State_name
                    };
                });
        */      
    });
    IMapper mapper = config.CreateMapper();
        
    var employee = new Employee{name = "Mark", city_name = "City A", State_name = "State A"};

    var employeeDto = mapper.Map<Employee, EmployeeDto>(employee);
}

Sample Solution 2 on .Net Fiddle


References

  1. ForPath
  2. Profile Instances
  3. Before and After Map
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.