Benjamin
Benjamin

新人工程師,偶爾分享工作心得(預計?

Work Essay - The Significance of Backend Layering

Out of the pack today.


A brief introduction to my work before I talk about what happened.

I am a rookie back-end engineer using the Java spring boot MVC architecture. Most of the back-end programs are classified into four categories:
model, which defines the field name and field variable type of various data.
repository, read and write the content of the database according to the definition of the model.
service, which performs logical judgments and operations on the data, and outputs error messages for exceptions if necessary.
controller, which defines the path of the back-end api, the method type of the connection, the parameter name and type of the input and output.

The process of the backend operation is as follows:
Someone sends a request to the back-end program that conforms to the rules. After the controller receives it, it calls one or more functions of the service. The service checks that the value of the data is correct and available, calls the repository to write/read the data, and then The result is returned to the controller, and the controller responds to the external request, and the process ends.

Yes, the user calls the controller, the controller calls the service, the service calls the repository, and the repository accesses data. This is the principle that should be followed. It should be avoided to call the functions of the same layer, resulting in unclear structure.


Today, after the program was launched from the test environment, there were some problems that needed to be corrected, but I found that I wrote it in a mess, and I discussed it with my seniors to find a solution.

The problem was my misuse of repository.

The repository is only responsible for execution. You say I do it. It should not judge the input value at this layer to determine what operation to perform, but call the repository function of a specific function after the service judges it.

"When the service calls the repository, it should be able to predict what operation will be performed this time, and the result is only normal and abnormal." This is the concept taught by seniors.

I set a function of the repository as [Update], input a string of numbers and compare it with another general table, delete the corresponding data for the missing part of the string of numbers, and add corresponding data for some parts, which can be said to be very confusion.

After improvement, the [Update] of the repository was split into two functions, [Add] and [Delete]. The service changed from a unified call to [Update] to a call to [Add] or [Delete] after judgment, which is convenient for management. too much.


Professional division of labor is really important.

CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...

Comment