“Diffing” objects from a relational database

后端 未结 12 1248
遇见更好的自我
遇见更好的自我 2021-02-04 15:05

Our win32 application assembles objects from the data in a number of tables in a MySQL relational database. Of such an object, multiple revisions are stored in the database.

12条回答
  •  你的背包
    2021-02-04 15:22

    Given you want to create a UI for this and need to indicate where the differences are, it seems to me you can either go custom or create a generic object comparer - the latter being dependent on the language you are using.

    For the custom method, you need to create a class that takes to two instances of the classes to be comparied. It then returns differences;

     public class Person
     {
         public string name;
     }
    
     public class PersonComparer
     {
         public PersonComparer(Person old, Person new)
         {
            ....         
         }
    
         public bool NameIsDifferent() { return old.Name != new.Name; }
         public string NameDifferentText() { return NameIsDifferent() ? "Name changed from " + old.Name + " to " + new.Name : ""; }
     }
    

    This way you can use the NameComparer object to create your GUI.

    The gereric approach would be much the same, just that you generalize the calls, and use object insepection (getObjectProperty call below) to find differences;

     public class ObjectComparer()
     {
        public ObjectComparer(object old, object new)
        {
            ...
        }
    
        public bool PropertyIsDifferent(string propertyName) { return getObjectProperty(old, propertyName) != getObjectProperty(new, propertyName) };
    
         public string PropertyDifferentText(string propertyName) { return PropertyIsDifferent(propertyName) ? propertyName + " " + changed from " + getObjectProperty(old, propertyName) + " to " + getObjectProperty(new, propertyName): ""; }
     }
    }
    

    I would go for the second, as it makes things really easy to change GUI on needs. The GUI I would try 'yellowing' the differences to make them easy to see - but that depends on how you want to show the differences.

    Getting the object to compare would be loading your object with the initial revision and latest revision.

    My 2 cents... Not as techy as the database compare stuff already here.

提交回复
热议问题