Deep Copy

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I know this should not be difficult but I'm stuck.

I have an object that's made up of several other objects. The details
don't matter but it's moderately complex. I have a form that users can
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.

I have done a deep copy of the object as it exists when the user
selects the form and that seems to be working OK. I'm stuck trying to
revert to the saved values when the user clicks the cancel button.

I know I can use brute force to run through all properties and assign
the values but this is cumbersome and I know that there must be a
better way. Suggestions appreciated.

Steve
 
I assume that you have some way of binding to the original object, i.e. when
the screen cups up it must read data from the object and display it just
redo this with the copy? (if not this might be your problem:))
 
Steve said:
I know this should not be difficult but I'm stuck.

I have an object that's made up of several other objects. The details
don't matter but it's moderately complex. I have a form that users can
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.

I have done a deep copy of the object as it exists when the user
selects the form and that seems to be working OK. I'm stuck trying to
revert to the saved values when the user clicks the cancel button.

I know I can use brute force to run through all properties and assign
the values but this is cumbersome and I know that there must be a
better way. Suggestions appreciated.

Steve

If you are looking for a simple Undo mechanism you should look at the
Memento pattern:
http://en.wikipedia.org/wiki/Memento_pattern
http://www.dofactory.com/Patterns/PatternMemento.aspx

You can typically implement that very fast using serialization.

HTH,
Andy
 
Steve said:
select when they wish to make changes to the object properties. For a
number of reasons, I want to commit the changes immediately as they are
made by the user (largely because of the impact on other calculations).
However, I want to give users the opportunity to Cancel changes and
revert to the values that existed before they displayed the form.

Where do you want to commit the changes? To a database? If that is
the case, you can just roll back the transaction if the user clicks the
Cancel button.
 
Yes, look into the Memento Design Pattern.
http://www.dofactory.com/Patterns/PatternMemento.aspx

If your objects are serializable, then you can try this:




public class CloneHelpers
{
public static object DeepClone(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;
return b.Deserialize(m);

}
public static bool DeepEquals(object objA,object objB)
{
MemoryStream serA = serializedStream(objA);
MemoryStream serB = serializedStream(objB);
if(serA.Length!=serA.Length)
return false;
while(serA.Position<serA.Length)
{
if(serA.ReadByte()!=serB.ReadByte())
return false;
}
return true;

}
public static MemoryStream serializedStream(object source)
{
MemoryStream m = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(m, source);
m.Position = 0;

return m;
}
}
 
Back
Top