Map into existing object
I'd like to be able to map into an existing object.
Use-case: I get the entity from the database, I have the model posted into the action. I don't want to replace the entity, just update those properties I got from the model.
Proposed:
public void MapToExistingDestination(this Source source, Destionation destination)
{
destination.Name = source.Name;
destination.Reason = source.Reason;
}
Then in my controller action (or service) I can have code like this:
[HttpPut("{id}")]
public ActionResult<Model> Put(int id, [FromBody]CustomerModel model)
{
Customer customer = customerRepository.GetById(id);
if (customer == null)
{
return NotFound();
}
model.MapToExistingCustomer(customer);
customerRepository.Save(customer);
return model;
}
This is something I'm considering with this issue. If it's done (I accept PRs ;) ), maybe the right thing to do is to generate a parameter of the destination type, rather than a Func<DestinationType> as you suggested.
Interesting. I can see value in both approaches. In my use-case I'm specifically copying properties between objects. In #12, the use-case is still creating a new one but overriding initialization. Are these use-cases different enough to justify API surface for each? Hm...