Copy object to object (with Automapper ?)

前端 未结 5 1859
[愿得一人]
[愿得一人] 2020-11-30 04:16

I have a class:

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

I have two insta

相关标签:
5条回答
  • 2020-11-30 04:54
    Mapper.CreateMap<Person, Person>();
    
    // Perform mapping
    
    var person1 = Mapper.Map<Person, Person>(person2);
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-30 04:55

    Since you asked With Automapper? can I suggest you don't use AutoMapper?

    Instead use MemberwiseClone() in a Clone method, e.g.

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
        public Person Clone()
        {
            return (Person) MemberwiseClone();
        }
    }
    

    UPDATE

    Its important to note this does not acheive the original posters desire to copy person1 into person2

    However, (and as @Jimmy Bogard points out) using MemberwiseClone() is preferred if you just need to make a copy (clone) of the object.

    For instance, if you are doing this:

    //I need a copy of person1 please! I'll make a new person object 
    //and automapper everything into it!
    var person2 = new Person2();
    Mapper.Map<Person, Person>(person1, person2)
    

    then really you should/could use

    //oh wait, i can just use this!
    var person2 = person1.Clone()
    
    0 讨论(0)
  • 2020-11-30 04:56

    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.

    0 讨论(0)
  • 2020-11-30 05:08

    Why do you want to use Automapper for this? A simple clone would do the job for you.

    Read more here: Deep cloning objects

    0 讨论(0)
  • 2020-11-30 05:11

    In the current version of AutoMapper, you can't use the static AutoMapper.Mapper.Map method. Instead, initialize a new mapper like this:

    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<Person, Person>();
    });
    
    var mapper = new Mapper(config);
    
    var clone = mapper.Map<Person>(person);
    

    Usually, you want to register the mapper in the Startup.cs file for dependency injection and inject it in your business class:

    public void ConfigureServices(IServiceCollection services)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Person, Person>();
        });
    
        var mapper = new Mapper(config);
    
        services.AddSingleton(mapper);
    
        // ...
    }
    

    Important: Don't create or inject the mapper in your entity class!

    Of course, you should prefer to use MemberwiseClone() in simple cases.

    0 讨论(0)
提交回复
热议问题