I have participated in the development of a radio/TV project. The project adopts the popular framework for system architecture Struts2 + sping + hibernate, the package structure of the project is divided into three layers: Action, Service, and dao. The Action layer interacts with the web interface, receives commands sent from the interface, and then displays different interfaces; service layer Service specific transaction processing, Spring transaction configuration at this layer, this layer provides services for the Action layer; Dao layer is to deal with the database for data access. At the same time, it can be divided into three layers to reuse classes or functions. However, in actual development, the technical layers of the project members are not consistent, most of which are just graduated or have been working for one year. Most of the Code is concentrated on the Action layer, and the Service layer and Dao layer are arranged.
In my opinion, in these three layers, the core layer should be the Service layer. No matter how the Action layer and Dao layer Change, as long as the processing process of this function remains unchanged, the methods at the Service layer should not change. Read the original code and find that the Service method is called multiple times in an Action method. That is to say, the logic in a transaction should be distributed to multiple transactions, unless necessary, most of the time, an operation is a transaction.
In addition, can I write SQL statements in the Service? I think this is a detailed analysis. For example, to query data in the simplest way, you need to search for conditions based on whether or not the parameters are available. The query conditions can be regarded as business logic. Therefore, the SQL statements for query conditions can be placed on the service layer. For example, to obtain data based on the ID, this SQL statement should be on the Dao layer. The Service layer only needs to determine whether the ID is null.
The purpose of the layer-3 structure is to reuse functions. Therefore, the interfaces of the Service layer and Dao layer must be defined as well as possible, otherwise it is difficult to achieve reuse. How can the interface be well defined? I think we need to constantly refactor the code. In the past few days, the function of a subsystem in the expansion project has been found to be too long. The longest one actually occupies three and a half screens. When I saw this code, I first thought of code refactoring. I browsed it and found that there were repeated codes. If your code is too long, you must divide it into several methods for implementation. A method that is too long is hard to be viewed and cannot be modified.
I talked about some issues in development. How can I avoid these problems and how can I ensure that the three layers have their respective roles?
First, code should be read regularly during project implementation to detect problems in a timely manner. This should be done by the project leader.
2. Developers check each other and communicate with each other in this regard. problems can be found through communication to find a better way.
3. Develop code specifications