3

Current situation

I have a WPF application where I use MVVM and NHibernate. I have a master/detail window with a listbox with all my customers, and the selectedItem of the listbox is the object that is being used to display the customerdetails in the detailscreen. In the detailscreen I have an add, edit, delete, save and an undo buttton. Everything works with the binding I've set up.

Problem

But for my undo button I was thinking of making a copy of the original Customer object so when I click the undo button the field will be resetted to the values from the original values. But in my customer object I have an Address object and with a shallow copy the 2 objects will keep the same reference to that object. So when I change a field from the Address object the original Customer address will also be changed. I was thinking of doing a deep copy of my Customer address but I can't make my object serializable (It's not in my control to change the Model objects)

  • Is there any way to do a deep copy without serialization?
  • Or is there some standard way to accomplish the behavior I want to achieve?
koala
  • 1,544
  • 1
  • 20
  • 33

1 Answers1

2

I would simply go back to the database and reload the customer object. That ensures that the data you're displaying is consistent with the data in the database and reduces the risk of concurrency issues.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • I haven't thought about it in that way, but in my opinion this is not best practice? But I think it will definitely do the job for me, I will try it out – koala Mar 06 '13 at 18:26
  • IMO showing the user fresh data is the best practice unless there is a significant performance reason to do otherwise. It's also much easier to code and less prone to bugs. – Jamie Ide Mar 06 '13 at 18:30
  • I've tried it but I notice that when I go back to the database and I want to get that same object to have a copy of it, It seems that I don't have a second seperate object with the old values. I think nhibernate knows that I'm using that object already and just uses that one... any thoughts about this? – koala Mar 08 '13 at 13:04
  • If you're re-loading in the same ISession then NHibernate will get the object from the cache instead of the database. To force it to go back to the database, call session.Evict(myObject) and then reload. See also http://stackoverflow.com/questions/4106554/whats-the-best-way-to-refresh-entities-in-nhibernate. – Jamie Ide Mar 08 '13 at 13:23
  • Thanks you've put me into the right direction. I have used the session.Refresh(myObject) and everything is working now! :) – koala Mar 08 '13 at 13:27