Daniel Eugen
Daniel Eugen

Reputation: 2780

AutoMapper Map object to array

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.

Upvotes: 2

Views: 2611

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

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);

Upvotes: 2

Related Questions