Update an object's unpopulated members with values from another using:
Mapper.Map(customerViewModel).OnTo(customer);
When merging collections, objects are matched by id (configurable if necessary). For example:
var source = new Collection<CustomerViewModel>
{
new CustomerViewModel { Id = 1, Name = "Rod" },
new CustomerViewModel { Id = 2, Name = "Jane" },
new CustomerViewModel { Id = null, Name = "Freddy" }
};
var target = List<Customer>
{
new Customer { CustomerId = 2, Name = null },
new Customer { CustomerId = 1, Name = "Bungle" },
new Customer { CustomerId = 3, Name = "Zippy" }
};
var result = Mapper.Map(source).OnTo(target);
In this case:
source[0]
is matched totarget[1]
, buttarget[1].Name
is already populated, so it isn't changedsource[1]
is matched totarget[0]
, sotarget[0].Name
is changed fromnull
to 'Jane'source[2]
has no match in the target collection, so a newCustomer
is added withCustomerId
set tonull
andName
set to 'Freddy'target[2]
(CustomerId
= 3) has no match in thesource
collection, so it isn't changed- The original
List<Customer>
instance is maintained and assigned to theresult
variable