I am part of a team that is developing a web application using a proprietary mvc framework. This framework behaves a bit like a very poor man's struts! It has a central controller, configuration in properties file, torque like generated database action classes and no form beans.
Say you have a user account editing screen, Here is a typical piece of code that developers are writing: public class Handler extends StupidFrameworkBaseHandler{
// This is analogous to Struts Action
// Handlers are invoked by your controller.
public void execute() {
// get form elements
}
}
}
To introduce the concept of form beans, an abstract getFormBean() method was declared extending the StupidFrameworkBaseHandler class. This method would be invoked by the BaseHandler class. So for now the code looks like this:
public class Handler extends ExtendedStupidFrameworkBaseHandler {
public void execute() {
UserEditFormBean bean = (UserEditFormBean) baseBean;
// business layer classes.
}
public BaseBean getFormBean() {
// get HTTP Request parameters and build an object.
UserEditFormBean bean = new UserEditFormBean();
bean.setUser(httpRequest.getParam("whatever"));
// other setters...
}
return bean;
}
In earlier applications that were developed using this framework, coders would code everything into the Handler - getting db connection, business logic etc. In order to change that the concept of business and DAO layer was introduced in my current application.
So final code looks a bit like this:
public class Handler extends ExtendedStupidFrameworkBaseHandler {
public void execute() {
UserEditFormBean bean = (UserEditFormBean) baseBean;
UserBO busObj = new UserBO();
busObj.validateUserDetailsAndSave(bean); // I know this sucks..
}
public BaseBean getFormBean() {
// grab user input from form and return a form bean instance.
}
}
And the business layer looks like this:
public UserBO extends BaseBO {
public void validateUserDetailsAndSave(UserEditFormBean bean) {
UserDAO dao = factory.getDao("user");
// call getters on bean, do some validations, throw business exceptions.
User objUser = new User();
object.setUserName(bean.getUserName());
// few more setters here on User -> that is the model.
dao.update(objUser);
}
}
I find this overall design HORRIBLE, for many reasons:
- UserBO is a class that has no state. It is just a set of methods with procedural code.
- If you are calling these as layers, then each layer shouldn't depend on the other: UserBo would always expect one type of FormBean for each method and if I were to ignore HTML and re use the same BO class from a standalone java application, I would never be able to without creating FormBeans as params.
- Code looks horrific and unmaintaineable.
So, my question here would be:
Shouldn't you develop a business layer independent of your presentation layer? I would rather code my business objects with proper state, validation and exception throwing?
Isn't the code provided as example above, really bad non-Java way of doing it?