Dmehro
Dmehro

Reputation: 1289

Automapper mapping nested objects

I have following classes structure

public class ClassA
{
    public ClassB objB;
}

Public class ClassB
{
    public ListOfData objListofData;
}

public class ListOfData 
{
    public Employee objEmp;
}

public class Employee
{
    public string FirstName;
    public string LastName;
}

Return type of above hierarchy would be

"ClassA":{
    "classB":{
        "ListOfData":{
            "employee":{
                "FirstName":"David",
                "LastName" :"Peter"
            }
        }
    }
}

I want to map employee class to EmployeeViewModel where Employee firstname and lastname will be mapped to employeeViewModel FullName property. I can achieve this by following piece of code

public class EmployeeViewModel
{
    public FullName;
}


CreateMap<Employee,EmployeeViewModel>()
                         .ForMember(dest => dest.FullName,
                                    opts => opts.MapFrom(
                                        src => string.Format("{0} {1}",
                                        src.FirstName, src.LastName)));

Now How do I return back Original classA object which has mapping of EmployeeViewModel. Something like this ??

"ClassA":{
    "classB":{
        "ListOfData":{
            "EmployeeViewModel":{
                "FullName":"David Peter"
            }
        }
    }
}

Upvotes: 4

Views: 6565

Answers (1)

Dmehro
Dmehro

Reputation: 1289

After spending good amount of time reading documentation , I found solution here is my approach

a)Create Class source & destination classes like this

public class ClassA
{
    public ClassB objB;
}

Public class ClassB
{
    public ListOfData objListofData;
}

public class ListOfData 
{
    public Employee objEmp;
}

public class Employee
{
    public string FirstName;
    public string LastName;
}
------------------------------------
public class DestClassA
{
    public DestClassB objB;
}

Public class DestClassB
{
    public DestListOfData objListofData;
}

public class DestListOfData 
{
    public DestEmployee objEmp;
}

public class EmployeeViewModel
{
    public string FullName;
}

b)Create mapping

var config = new MapperConfiguration(cfg => {

    cfg.CreateMap<Employee, EmployeeViewModel>().
                ForMember(dest => dest.FullName, opts => opts.MapFrom(
                    src => string.Format("{0} - {1}",
                    src.FirstName, src.LastName)));
    cfg.CreateMap<ListOfData, DestListOfData>();
    cfg.CreateMap<GetFundFamilyOutOfBalanceTotalsResponse, GetFundFamilyOutOfBalanceTotalsResponseMapped>();
    cfg.CreateMap<ClassB, DestClassB>();
    cfg.CreateMap<ClassA, DestClassA>();
});

//This is to make sure your mapping is correct

config.AssertConfigurationIsValid();

//You can test if expected objected is created by automapper, add following code after mapping

var mapper = config.CreateMapper();
var output = new ClassA
{
    objClassB = new ClassB
    {
        objListofData = new ListOfData 
        {
            Employee = new []
               {
                    new Employee  { FirstName = "David", LastName ="Peter" }
               }
        }
    }
};

var destMap = mapper.Map<DestClassA>(output);

//if you explore this object you will find object struture like this

"ClassA":{
    "classB":{
        "ListOfData":{
            "EmployeeViewModel":{
                "FullName":"David Peter"
            }
        }
    }
}

Upvotes: 6

Related Questions