spring - Generic DAO - "Never go full generic!" -
ive seen allot of usages of generic dao on internet. gotta love it:
public interface genericdao <t, pk extends serializable> {} public class genericdaohibernateimpl <t, pk extends serializable> implements genericdao<t, pk>
a new class appeared? no problem:
public interface newclassdao extends genericdao<newclass, long>
and we're set.
now, how bad if go "full-generic" , like:
public interface fullgenericdao public class fullgenericdaohibernateimpl
now can use 1 dao (of object casting!)
a new class appeared again? no problem:
newclassapperedagain newclassappeared = (newclassapperedagain) fullgenericdao.getitembyid(20, newclassapperedagain.class);
two questions comes mind:
1) possible go "full-generic" , create such dao? mean dont see why not passing every method of dao classname , casting neccessary? save(object,classname); delete(object,classname); etc..
2) cons (i'm sure there are) of such practice?
thanks!
dao objects there contain domain model specific logic connected persistence layer.
you can go full generic, have face questions like:
- where should put logic connected searching (e.g. constructing clause)?
user finduserbyorganizationid(serializable organizationid)
- where put specific mapping functions (e.g. aggregate queries)?
map<organization, integer> findusercountperorganization()
i pretty sure there other architectural / logical issues. generics erased during runtime have introduced necessity include domain object class<?>
in every method signature.
tl;dr possible "go full generic", need move non-shared persistence logic higher levels. better shared generic logic in abstractdaoimpl
, subclass that. might end empty implementations (the generic superclass might have need), have place future domain model specific methods.
Comments
Post a Comment