HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Layered architecture implementation in a Java web application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
architecturelayeredapplicationjavawebimplementation

Problem

I am trying to learn how to design and implement a layered Java web application with a presentation layer, service layer and the DAO layer. I have used the Struts 1.3 framework with Java JDK 1.6 for the below explained sample project.

Questions:

-
How GOOD or BAD is the below implementation of the layered web application architecture? Is this the correct way to implement a layered architecture in a Java web application? Is this implementation following MVC strictly?

-
Is this the correct way of propagating exceptions from one layer to another?

-
Where should logging be done: in all 3 layers or for every request?

-
Is the UserDTO object used correctly in this code? I have written the bean data copy code in the formbean, to be used when sending data from presentation layer to service layer and then from there to the DAO layer. Is it okay? Where should the corresponding code for copying User data from a resultset to the DTO object in DAO layer be implemented?

-
Also, PostgreSQLDAOFactory.java contains 2 static getData(..) methods. Since they are DB independent, where should I put them so that any DB implementation of JDBC DAO code can access them? Can they be static methods? If yes, should they be synchronized methods or with synchronized code blocks?

-
Since every request is processed by a new thread in Java web application should the service and DAO layer methods be synchronized so that there are no concurrency problems and lack of thread-safety?

-
Is it standard to use different packages for the interfaces and concrete implementations? If not, what is the standard layout of the packages in a Java EE web app with such layered architecture?

-
In UserServiceImpl.java file I am setting the datasource type to PostgreSQL. If the datasource type changes, doing this way I'll have to change this setting in many such ServiceImpl files (for example, ProductServiceImpl, CustomerServiceImpl, etc.). What is the standard way to set the datasource type

Solution

DAOFactory

public abstract class DAOFactory {
    // List of DAO types supported by the factory
    public static final int POSTGRESQL = 1;
    public static final int ORACLE = 2;

    ...
}


Woah, hold your horses! You should be using enums here, instead of plain int values. Plain int values for this scenario are ripe for abuse when you accidentally switch against 11 instead of 1, and you can say goodbye to an hour of additional sleep at 3 am while debugging furiously.

PostgreSQLUserDAOImpl

Under addUser(UserDTO):

if (intCount > 0)
        return true;
    else
        return false;


The above block can be simplified as return intCount > 0. Slightly nitpicking on this, but the variable name strBldr4Qry in fetchUserRolesList() doesn't quite roll off the tongue... perhaps you are looking for queryBuilder?

edit: For the following method which is implemented by this class:

public interface UserDAO {
    public Collection> fetchUserRolesList();
}


Is there any reason why you are using Object as the generic type for the List? Maybe you need a domain class for it as well?

Other observations:

-
Your braces-style is extremely inconsistent, even across classes, not to say the entire codebase. I will strongly suggest standardizing the style as consistent code readability is beneficial in the long run.

-
If you are on Java 7, I think applying try-with-resources will greatly reduce the amount of boilerplate-like resource-handling code you have. If you are on Java 8, I foresee you might even be able to do some stream-based processing on the loops, or even use a library like jOOQ to simplify parts of your code.

Code Snippets

public abstract class DAOFactory {
    // List of DAO types supported by the factory
    public static final int POSTGRESQL = 1;
    public static final int ORACLE = 2;

    ...
}
if (intCount > 0)
        return true;
    else
        return false;
public interface UserDAO {
    public Collection<List<Object>> fetchUserRolesList();
}

Context

StackExchange Code Review Q#104363, answer score: 5

Revisions (0)

No revisions yet.