Update an object's members with values from another using:
Mapper.Map(customerViewModel).Over(customer);
When updating a collection, objects are matched by id (configurable if necessary). For example:
var source = new[]
{
new CustomerViewModel { Id = 1, Name = "Rod" },
new CustomerViewModel { Id = 2, Name = "Jane" },
new CustomerViewModel { Id = null, Name = "Freddy" }
};
var target = Collection<Customer>
{
new Customer { CustomerId = 2, Name = null },
new Customer { CustomerId = 1, Name = "Bungle" },
new Customer { CustomerId = 3, Name = "Zippy" }
};
var result = Mapper.Map(source).Over(target);
In this case:
source[0]is matched totarget[1], sotarget[1].Nameis updated to 'Rod'source[1]is matched totarget[0], sotarget[0].Nameis set to 'Jane'source[2]has no match in the target collection, so a newCustomeris added withCustomerIdset tonullandNameset to 'Freddy'target[2](CustomerId= 3) has no match in thesourcecollection, so it is removed- The original
Collection<Customer>instance is maintained and assigned to theresultvariable