Detecting changes for "Save" and "Save As..." purposes

I have a base abstract class that contains a generic dictionary. All of the derived concrete classes inherit this dictionary. The Value of the dictionary is of the base class Type. This is to allow adding/deleting of derived types.

Ok, what I would like to do, is detect when changes have been made in the object model, so that I can implement a "Save" and "Save As..." method. What is available out there that allows me to do something like this





Answer this question

Detecting changes for "Save" and "Save As..." purposes

  • esb

    I'm well aware that I can roll through the object model and compare properties. I was wondering if there was a standard shortcut to avoid having to do this. I have 100's of properties that I would have to check for...


  • Zakamon

    If you don't care for efficiency, and your classes are serializable, you could always serialize the classes to a MemoryStream and compute a hash (MD5, SHA1, take your pick). It is theoretically possible that two configuration files share the same hash, but the probability is negligible. While this is probably as inefficient as it gets, it is at least short to write, which seems to be your main requirement.

    HTH
    --mc


  • ygermain

    On methods or properties in your domain model, you add Dirty flags. A Dirty flag indicates whenever a object is changed, here are two example:


    public String FirstName
    {
    get
    {
    return _firstname;
    }
    set
    {
    if( !_firstname.Equals( value ) )
    {
    _firstname = value;
    MarkDirty();
    }
    }
    }

    public void Add( OrderLine line )
    {
    _innerCollection.Add( line );
    MarkDirty();
    }




  • Alexey Vishnyakov

    That's fine for strings, but does this work for reference objects I thought .Equals() only compares memory addresses. Which, if this is the case:

    Robot r = new Robot();

    Robot r2d2 = r;

    r.Add(new Arm("left");

    r.Arms["left"].length = 23;

    The following would result in true:

    r.Equals(r2d2)



    If I'm misunderstanding something here, please tell me, but that is how I understand this.


  • texan149142

    And, although I don't recommend the following idea, if you really have many properties that you want to compare. Think about putting them into any array, or Collections, for example, like a Hashtable.

    Basically, you can put all your object properties in a hashtable, which is a prive member of the class. Next is to hide and encapsulate it with Get Set method for the properties. When doing comparison in the Equals() method, what you have to do is loop through the hashtable, and compare the value one by one.

    But in terms of design, I am not sure whether it is a "best practice".



  • Daffodils

    The best way to handle this would be to have Class1 and Class2 implement INotifyPropertyChanged (which is good standard practice anyway). Make sure all classes which implement Class2 raise this event appropriately. Then you can have your top-level classes "watch" all their "child" objects, by attaching an event handler to the PropertyChanged event, and set a dirty flag whenever that handler is called.

  • Jehan Badshah

    referencing memory addresses is usually == than .Equals. .Equals() checks for object equivelence (if they are the same object)

  • FandangoAmeruso

    I don't think there's any kind of short cut.

    In http://msdn2.microsoft.com/en-us/library/bsc2ak47.aspx

    Notes to Implementers This method can be overridden by a derived class. For example, many of the base data types return true if both objects represent the same value; otherwise, false. This method only compares primitives and objects. It must be overridden to compare more complex structures, such as arrays of objects. The following statements must be true for all implementations of the Equals method. In the list, x, y, and z represent object references that are not a null reference (Nothing in Visual Basic).

    Hope this can answer your problem.



  • sl5

    Basically, I have two main classes in my object model, let's just call them Class1 and Class2. Each class contains a Generic Dictionary with the value being that of the other class. This is so that users can call a .Add() method and are able to add Class1 objects to Class2 objects and vise versa. Class2 however is an abstract class, which has 12 classes that inherit from it. Each one of these 12 classes however have their own unique characteristics.

    So, as you can see, this would prove rather difficult rolling through and comparing all of the data in each one of the classes. That is why I was wondering if there was some shortcut method to detect changes.

    Hopefully this gives everyone a better concept of my question.


  • Tryin2Bgood

    For your custom object type, you have to implement the Equal() method yourself to tell in what condition the two objects of that class is equal.

    public class Robot
    {
            private string name = "";

            public override bool Equals(object obj)
            {
                // write your code here
                // return true if equals, fasle if not
               
               // for example 
               return (this.name == ((Robot)obj).name) ; 
           }
    }

     

    Reference:
    ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_csref/html/7e4c24c5-7693-4c45-88fb-ba5204fbcb20.htm



  • Zooz

    ok, but in the example that I just provided, wouldn't that mean that these were the same objects


  • Detecting changes for "Save" and "Save As..." purposes