4

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"
            }
        }
    }
}

1 Answer 1

6

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"
            }
        }
    }
}
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.