At least for some common patterns like DAOs.
As a popular manner, most of the java applications have at least three layers like UI, Service and DAO. For each layer you probably have interface and a single or more implementation classes. And while UI layer has some really complex businesses for effective UI experience, DAO layer is only related with CRUD operations for a single or a group of beans.
Here is the routine that you can get rid of
eg
public interface UserElementDAO {
public User create(String username,String password,String email);
public User updatePassword(String username,String password);
public User updateEmail(String username,String email);
public List<User> findUsers(String email);
}
After this implementation there is no need to give an extra information for developer. But you still need the implementation ? No not necessary here is the RECIPE : more Annotation and BeanFactory
Let’s give some more BINARY information (annotation)
@Repository
public interface UserElementDAO {
@DAOMethod(type=DAOMethodType.SAVE)
public User create(
@DAOMethodParameter(property="username")String username,
@DAOMethodParameter(property="password")String password,
@DAOMethodParameter(property="email")String email);
@DAOMethod(type=DAOMethodType.UPDATE)
public User updatePassword(
@DAOMethodParameter(property="username")String username,
@DAOMethodParameter(property="password")String password);
@DAOMethod(type=DAOMethodType.UPDATE)
public User updateEmail(
@DAOMethodParameter(property="username")String username,
@DAOMethodParameter(property="email")String email);
@DAOMethod(type=DAOMethodType.SEARCH)
public List<User> findUsers(
@DAOMethodParameter(property = "email",operator=DAOMethodOperator.LIKE)String email);
}
NOW IT IS ALSO MORE MEANINGFUL FOR YOU NOT ONLY DEVELOPER BUT ALSO THE APPLICATION ITSELF
After this point what you will do is to generate a dynamic Proxy with the help of spring BeanFactory
and inject it where it is necesary.
Example implementation for hibernate:
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy)
throws Throwable {
String name = method.getName();
// first check if it a an implementation
if (name.equals("toString")) return targetDAOImpl.getClass().getName() +
" Proxy";
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("method:" + name);
LOGGER.debug("args = " + Arrays.asList(args));
}
if (Modifier.isAbstract(method.getModifiers())) {
DAOMethod annotation = method.getAnnotation(DAOMethod.class);
if(annotation==null){
LOGGER.info("GenericHibernateDAOFactory.no handler"+method.getName());
return null;
}
// method is abstract choose method handler
if(annotation.type().equals(DAOMethodType.SAVE)){
return saveMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.UPDATE)){
return updateMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.DELETE)){
return deleteMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.SEARCH)){
return searchMethod(obj,method, args,proxy);
}else if(annotation.type().equals(DAOMethodType.COUNT)){
return countMethod(obj,method, args,proxy);
}
}
return proxy.invokeSuper(genericHibernateDAO, args);
}
/**SAVE METHOD HANDLER*/
private Object saveMethod(Object obj, Method method, Object[] args,
MethodProxy proxy)
throws InstantiationException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
LOGGER.debug("GenericHibernateDAOFactory.intercept");
Object o = objectClass.newInstance();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] parameterAnnotation = parameterAnnotations[i];
if(parameterAnnotation ==null)continue;
for (Annotation annotation1 : parameterAnnotation) {
if (annotation1 instanceof DAOMethodParameter) {
DAOMethodParameter daoMethodParameter = (DAOMethodParameter) annotation1;
String s = daoMethodParameter.property();
PropertyUtils.setProperty(o, s, args[i]);
}
}
}
getSession().save(o);
return o;
}
Any opinions or suggestions are always welcome
Related posts:
Omar said on Friday, October 3, 2008, 13:33
I was looking for something like a Generic Service Layer because in a recommended implementation we are using [recommended by an architect who has Framework fever].
The recommendation consists of 4+ layers, The service layer was splitted in 2 additional layers, Business Service (BS) and Business Objects (BO), so this always lead us to have a robust BS and a forwarding BO layer. I think annotations could be a solution for not to break this “recommendation” but to save some development time.
Personally I still don’t buy the fact that not to implement my DAO layer but it seems interesting, I’m going to give a try on this.
altuure said on Tuesday, October 7, 2008, 6:53
Hi Omar,
I do use such a frame for my own prototype application it is %90 sufficient for any ORM business and less code means less coding error
i hopefully submit a proof of concept implementation as a small open source project
good luck to you too
altuure said on Tuesday, October 28, 2008, 5:00
Hi every body,
actually here I want the discuss the idea and approach not the sample draft implementation
all comments that would add value would welcome