Skip to content

Map into existing object #28

Description

@robrich

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions