I believe in OO, but not to the point where inappropriate designs/implementations should be used just to be "OO Compliant".
So, how to deal with the Serlvet/EJB/DataContainer layered architecture:
- Servlets receive requests and call a "business layer" (e.g. session EJBs)
- The business layer locates DataContainers from the database and manipulates them to implement the business logic
- DataContainers contain no real code, just get/set corresponding to the database.
This approach has appeal; the DataContainers are clear in what they do and it's very easy to know where data comes from.
Aside from not being OO, this leads to unclear Business Layer classes that can be hard to name and hard to organize.
Even if we were trying to be more "OO" (e.g. putting some of these methods in the DataConatiners), some of these operations operate on more than one set of data.
How do you keep your Business Layer from getting confusingly procedural, but without polluting your DataContainers with business logic?
Example
class UserServlet {
handleRequest() {
String id = request.get("id");
String name = request.get("name");
if (UserBizLayer.updateUserName(id,name))
response.setStatus(OK);
else
response.setStatus(BAD_REQUEST);
}
}
class UseBizLayer {
updateUserName(String id, String name) {
long key = toLong(id);
user = userDAO.find(key);
if user == null
return false;
if (!validateUserName(name))
return false;
user.setName(name);
userDAO.update(user);
return true;
}
validateUserName(String name) {
// do some validations and return
}
}
class User {
long key;
String name;
String email;
// imagine getters/setters here
}
- We don't want
validateUserName
on the user, since it only operates on a name; I guess it could go into another class, but then we have another procedural "uti" type class - We don't want persistence methods on the User, since there's value in decoupling data structures from their persistence strategy
- We don't want business logic in our Servlet, since we may need to re-use that logic elsewhere
- We don't want our business logic in our User, as this draws in way too much to the User class, making re-use of the business logic difficult and coupling the user with its persistence strategy
I realize this example isn't that bad, but imagine 10 DataContainers and 20 BizLayer objects with several methods each. Imagine that some of those operations aren't "centered" on a particular data container.
How do we keep this from being a procedural mess?