2

I have an entity called ActionResult that my methods along the application returns. Now i want to map a returned object in the ActionResult to it's right place in an array of that object...

public class Core
{
    public Employee[] Employees = new[] {
        new Employee {
            Name = "Jack",
            Age = 21,
            Salary = 1000
        },
        new Employee {
            Name = "Carl",
            Age = 35,
            Salary = 1000
        }, 
        new Employee {
            Name = "Tom",
            Age = 41,
            Salary = 1000
        }, 
    };
}

public class ActionResult
{
    public string ActionID { get; set; }
    public Employee Employee { get; set; }
}

public class Employee
{
    public String Name { get; set; }
    public int? Age { get; set; }
    public int? Salary { get; set; }
    public int? Level { get; set; }
}

public ActionResult MethodThatReturnsActionResultWithAnEmployee()
{
    return new ActionResult {
        ActionID = new Guid().ToString(),
        Employee = new Employee {
            Name = "Carl",
            Age = 35,
            Salary = 7000,
            Level = 1
        }
    };
}

Now as you can see what i want to do is taking the Employee that is returned from the Method, and search in the array of Employees in the Core and update it using the new given data using AutoMapper.

1 Answer 1

2

AutoMapper will not search employee in some array for you. How it would know which employees should be considered as equal? You should search for employee manually, and use appropriate mapping method to update existing instance of employee with data from other employee instance:

Mapper.CreateMap<Employee, Employee>();
var result = MethodThatReturnsActionResultWithAnEmployee();
var employee = result.Employee;
var core = new Core();
var employeeToUpdate = 
    core.Employees.FirstOrDefault(e => e.Name == employee.Name);
Mapper.Map(employee, employeeToUpdate);

If you really want mapping to look like

Mapper.Map(result, core);

Then you should write your own type mapper for this:

public class ActionResultToCoreConverter : ITypeConverter<ActionResult, Core>
{
    public Core Convert(ResolutionContext context)
    {
        var result = (ActionResult)context.SourceValue;
        var employee = result.Employee;
        var core = (Core)context.DestinationValue ?? new Core();
        var employeeToUpdate = 
            core.Employees.FirstOrDefault(e => e.Name == employee.Name);
        Mapper.Map(employee, employeeToUpdate);
        return core;
    }
}

And mapping will look like:

Mapper.CreateMap<Employee, Employee>(); // this map also required
Mapper.CreateMap<ActionResult, Core>()
      .ConvertUsing<ActionResultToCoreConverter>();

var result = MethodThatReturnsActionResultWithAnEmployee();
var core = new Core();
Mapper.Map(result, core);
// if you want to create new Core instanse:
var core2 = Mapper<Core>(result);
Sign up to request clarification or add additional context in comments.

2 Comments

i tried your provided help but unfortunately it does not map the rest of the properties... idk why
@DanialEugen in second approach if you have some properties on Core which should be mapped, then I think you should map them manually in type converter (because objects are mapped with custom type converter). All properties of Employee should be mapped without problems.

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.