Thursday, 15 April 2010

java - Automatic detection of correct map -



java - Automatic detection of correct map -

public class daohelper { private static final map<class, class> dto_to_dao_map; private static final map<class, class> acdto_to_acdao_map; static { dto_to_dao_map = new hashmap<class, class>(); dto_to_dao_map.put(employerdto.class, employerdao.class); acdto_to_acdao_map = new hashmap<class, class>(); acdto_to_acdao_map.put(academicdto.class, academicdao.class); } public static basedao<?> getdao(class dtoclass) { basedao<?> dao = null; class daoclass = acdto_to_acdao_map.get(dtoclass); //here //rest of code here }

class daoclass = acdto_to_acdao_map.get(dtoclass); in proportion of code, instead of acdto_to_acdao_map how can code observe automatically, don't manually each 1 dto_to_dao_map or others...

i suggest using naming convention , reflection:

assuming every dao (foodto) has corresponding dao (foodao) in same package, code (disclaimer - off top of head , may need tweek or 2 compile)

public static basedao<?> getdao(class dtoclass) { string dtoclassname = dtoclass.getname(); string daoclassname = dtoclassname.replaceall("dto$", "dao"); class daoclass = class.forname(daoclassname); retrun daoclass.newinstance(daoclass); }

and of course of study logic altered accommodate different naming conventions or different packages.

java static hashmap dao dto

No comments:

Post a Comment