• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Design for Dirty Check

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What would be a smart of checking if an object is dirty especially if am looking at only a subset of its properties for a change

Eg. class Person should be have a field updated if and only if 24 specific properties out of its 30 properties are modified

instead of defining a method like

private boolean hasChanged(Person p1 , Person p2){
if(!p1.getAttr1().equals(p2.getAttr1()) ||
!p1.getAttr2().equals(p2.getAttr2()) ||
!p1.getAttr3().equals(p2.getAttr3()) ...
......................................
return true;
}
return false;
}

I dont want to use reflection just to accomplish this. Is there a better design for checks like these?

Thanks
Menon
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, I don't like the name hasChanged() here, since you are actually comparing two different instances. A hasChanged() method implies that a single instance has changed its state since the last checkpoint, where checkpoints are defined in some way meaningful to your system. You could accomplish that sort of check by having every set method also flip on a "dirty" flag in the instance. In fact, we had to do something like this in the very early days of ORM, even with Entity Beans from EJB 1.0 if I remember right. Of course, you'll also have to have some way to turn off the dirty flag whenever that makes sense for your application.

What you've implemented looks more like an equals() method. Unfortunately, there's no better way to do that other than just writing out the code, but some IDEs have tools that will do the typing for you. I don't understand the 24 out of 30 requirement. Could you be clearer about what you're trying to do?
 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could create a Set for the specific properties.
Everytime the setAttr() methods is called on an attribute add it to the Set.
When the size of the Set is 24 you have all 24 attributes changed.

The name of the attribute can be used as the object for the Set.

When the time is right you can remove all the elements from the Set.

This approach is different from what you wanted.
But it can be a workaround.

Hope this is helpful.
 
Arundhathi Menon
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Greg & Preity.
Greg , you are right , what am comparing is the UI entity versus its old copy that was stored in the DB. They are different class types for sure , but have the same set of properties.
By 24 out of 30 I meant the dirty flag had to be set only when certain properties are updated and not all.

Preity , thanks for the input. I was trying to get to a generic way of coding this versus writing it all out in a block of If statement with multiple OR conditions.

Looks like the only way out.

Thanks again
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eg. class Person should be have a field updated if and only if 24 specific properties out of its 30 properties are modified


That isn't what your code indicates--your code is checking to see if *any* of the 24 specific properties have been modified, not iff 24 specific properties have been modified.

If the code is correct, the same thing could be done by creating a hash value of those 24 properties and comparing that against the hash of the possibly-modified object.
 
Get me the mayor's office! I need to tell him about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic