I have a simple problem try to stay DRY using Appengine.
The 2 functions below are identical except for the object sent as parameter. In reality I have 15 functions like this. I am try to find a way to create a super class or a generic to achieve this.
public void deleteRecord(Person s) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Person p = pm.getObjectById(Person.class, s.getId());
pm.deletePersistent(p);
} finally {
pm.close();
}
}
and
public void deleteRecord(Product s) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Product p = pm.getObjectById(Product.class, s.getId());
pm.deletePersistent(p);
} finally {
pm.close();
}
}
Unfortunately it seems that I cannot use generics since generics don’t support T.class.
Any good suggestion how to do this w/o duplicating?
Thank you.
Daniel
DRY is a good principle. So is KISS 😉
This would be called with, for example:
Since this is a void method, and PersistenceManager does not appear to work with generic types, I would advise avoiding using generics. This method, if applicable, has the added bonus that you won’t need to modify the type hierarchy of Product, Person, etc.
The downside is that if this method is called from several places, there may be many places to change the signature – but it’s easy to let the compiler find out how long it would take.