1

I have a class which contains:

[DataContract(Namespace = "Default")]    
public class Book
{
   ...
   some values
   ...
   [DataMember]
   public IList<Option> BookOptions {get; set;}
   ...
   some values
   ...
}

On some point in my programm, I need make a copy of existing book in database and make changes in it.

What am I doing:

var copy = (T)Activator.CreateInstance(typeof(T));
AutoMapper.Mapper.Map<T, T>(existBook, copy);
previousBook = copy;

AutoMapper.Mapper.Map(source, existBook, source.GetType(), typeof(T));

source - there are changes for book. Basically, I want to store in previousBook old data and in existBook get new one. Everything are working EXCEPT one thing: Any change in BookOptions for existBook, automatically change BookOptions in previousBook. In other words, previousBook and existBook has the same BookOptions IList... As I understand there exist reference or something similar (I''m a newbie so I can't say it more clearly).

So there is my question: How can I make a object copy without any references?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Olegs Jasjko
  • 2,128
  • 6
  • 27
  • 47

2 Answers2

0

With the line: previousBook = copy you create a reference to the object "copy". What you need is a deepclone: https://stackoverflow.com/a/129395

I would recommend you to read this article: http://www.albahari.com/valuevsreftypes.aspx

Community
  • 1
  • 1
kassi
  • 358
  • 1
  • 3
  • 10
0

Did you perhaps forget to CreateMap?

With the following code I was able to create a new separate instance:

public class Book
{
    public string Name { get; set; }

    public IList<string> Authors { get; set; }
}

//....

AutoMapper.Mapper.CreateMap<Book, Book>();

var book1 = new Book
{
    Name = "Original Book",
    Authors = new List<string>()
    {
        "Joe Blogs",
        "Jane Blogs"
    }
};

var bookCopy = AutoMapper.Mapper.Map<Book>(book1);

book1.Authors.Add("Me");

//book has 3 authors and bookCopy has the original 2

Try this:

previousBook = AutoMapper.Mapper.Map<Book>(existBook);

Now you can make changes to existBook and previousBook should remain unchanged.

Shane Haw
  • 723
  • 9
  • 22
  • No, I didn't. I have Mapper.CreateMap() in my MappingProfile.cs – Olegs Jasjko Jan 19 '15 at 08:27
  • @OlegsJasjko please would you post MappingProfile code? Also, are you sure that it is getting called? – Shane Haw Jan 19 '15 at 08:41
  • I just already did... CreateMap I mentioned is the one thing there which has connection with Book. Of course there are more CreateMap, for another classes. – Olegs Jasjko Jan 19 '15 at 08:43
  • @OlegsJasko I added something further, which you can try. – Shane Haw Jan 19 '15 at 08:49
  • nope, any changes in BookOptions in existBook still transfers to previousBook – Olegs Jasjko Jan 19 '15 at 09:44
  • @OlegsJasjko try and get all the different relevant pieces of your code together and replicate the problem in a small app. Then if the app still doesn't work then post that code. It is hard to see what is going on from the code snippets you have posted. – Shane Haw Jan 19 '15 at 09:59