Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy object to object (with Automapper ?)

Tags:

c#

automapper

I have a class:

public class Person {     public string FirstName { get; set; }     public string LastName { get; set; } } 

I have two instances of Person (person1 and person2). I'd like copy the contents of person2 to person1. I'd like to make this copy in one instruction and not property by property:

person1.LastName = person2.LastName; 

In the doc, I see copy an object to another object but the type is different. How can I copy the object when the type is the same ?

like image 717
Kris-I Avatar asked Apr 19 '11 08:04

Kris-I


People also ask

Does AutoMapper do deep copy?

The problem, as pointed out in the discussion of the above approach, is that AutoMapper does not do a recursive deep clone of nested objects: "...so you better make sure AutoMapper has mappings for member classes that you want to deep copy, and doesn't have mappings for member classes that you want to shallow copy."

What is the use of AutoMapper in C#?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.

Why you should not use AutoMapper?

1. If you use the convention-based mapping and a property is later renamed that becomes a runtime error and a common source of annoying bugs. 2. If you don't use convention-based mapping (ie you explicitly map each property) then you are just using automapper to do your projection, which is unnecessary complexity.


1 Answers

As I understand the question, OP does not want to clone person2 into a new instance of Person, but is asking for how to copy the contents of person2 into an already existing instance (person1) of Person. There is an overload of AutoMapper's Mapper.Map method that does this for you:

Mapper.CreateMap<Person, Person>(); Mapper.Map<Person, Person>(person2, person1); //This copies member content from person2 into the _existing_ person1 instance. 

Note 1: @alexl's answer creates a new instance of Person. If you have other references to the instance that person1 points to, these will not get the (presumably) desired data update if you redirect the person1 variable to a new instance.

Note 2: You need to be aware of that the (recursive) copying depth depends on what mappings AutoMapper knows about at the moment of mapping!
If a member of the Person class is of say the class Brain and you additionally have done Mapper.CreateMap<Brain, Brain>(); before the copy data Mapper.Map<Person, Person>(person2, person1); call, then person1 will keep its current Brain instance but this Brain will receive the member values of person2's Brain instance. That is you have a deep copy.
But if AutoMapper does not have a Brain-Brain mapping before copying, then person1's Brain member will reference the same Brain instance as the one person2 references. That is you will get a shallow copy.
This applies recursively to all members, so you better make sure AutoMapper has mappings for member classes that you want to deep copy, and doesn't have mappings for member classes that you want to shallow copy.

An alternative to using AutoMapper would be to use an approach using reflection. (Note that the code in the link does a shallow copy!)

"Support for filling an existing object, instead of AutoMapper creating the destination object itself" was added in AutoMapper version 0.2.

like image 88
Ulf Åkerstedt Avatar answered Oct 02 '22 09:10

Ulf Åkerstedt