0

I have a web application that allows the clerk to edit information. I copy an entity before the edit starts in case the user decides to cancel the changes. The problem is that any changes made on the copy is applied to the original object.

In C# I would create a deep copy to avoid that issue, but this application is using Entity Framework... I am not sure how to do a deep copy of an entity.


Here is more details on my problem... I am still trying to resolve.

I have a xaml screen with a grid binded to a list of inventory items. The items are an EntitySet. When I want the user edits one of the items, I copy the values of the current entity in an object "EntityToEdit" of the same type. The user makes a change, saves, and the list is automatically refreshed with the changes.

The problem occurs when the user selects another item to edit. That second item is somehow changed with the changes made on the first item....

How can I break the "link"?!?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • 2
    Not necessary, use [`DbEntityEntry.OriginalValues`](http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.originalvalues%28v=vs.113%29.aspx). – Gert Arnold Nov 04 '13 at 22:30
  • Here is more details on my problem... I am still trying to resolve. I have a xaml screen with a grid binded to a list of inventory items. The items are an EntitySet. When I want the user edits one of the items, I copy the values of the current entity in an object "EntityToEdit" of the same type. The user makes a change, saves, and the list is automatically refreshed with the changes. The problem occurs when the user selects another item to edit. That second item is somehow changed with the changes made on the first item.... How can I break the "link"?!? – user2953838 Nov 14 '13 at 13:31

1 Answers1

0

There is a lot of stuff going on in your question, I barely know where to start.

First, I do not recommend binding the Entities directly to your UI. Instead I recommend using some form of abstraction between your database and the presentation layer of your application.

There are a couple of established best-practices patterns that could be used, mostly depending on what technical environment you are working in. Have a look at the MVC pattern (Model-View-Controller) or at MVVM (Model-View-ViewModel).

Regarding your deep copy, there are lots of possible solutions. You can find some here at StackOverflow, like this one: "How do you do a deep copy an object in .Net?", where the objects are serialized into a Stream. Altough I, too, was using this kind of deep copy, but I try to avoid this. I see only very rare cases where a deep copy is really needed.

You should also consider implementing the [IEditableObject][4] interface on those objects that you want to edit. This allows you for an easy way to structure when and how your edited values are commited and to optionally or cancel or reset your edits by implementing straight forward methods like BeginEdit(), EndEdit(), and CancelEdit(). Some .NET controls that support this interface out-of-the-box and call these interface methods automatically if they exist on your objects.

By implementing IEditableObject you have full control of how the values are committed to which object. This will help you to avoid changing the original object accidentally.

Community
  • 1
  • 1
Jens H
  • 4,590
  • 2
  • 25
  • 35