java - 在SpringMVC的Controller层处理业务逻辑好吗?
天蓬老师
天蓬老师 2017-04-17 18:01:27
0
7
875

新接手的代码,发现在Controller里处理的逻辑就有几百行代码?又没有事务管理,这么做好吗?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(7)
阿神

Standard MVC does implement business logic directly in the controller, but in actual projects, it is still recommended to encapsulate the service layer between the controller and database operations.
On the one hand, the controller responds to different request URLs, so there will be a lot of duplication in functionality; it is difficult to maintain;
On the other hand, you must consider that your service functions may be exposed to other front-ends in the future, such as other application access, or different Terminals (such as APP, mobile H5, etc.); some services may even be separated and deployed independently;

洪涛

It’s very bad. It’s very difficult to expand and maintainability is also very poor.

The controller should be a thin layer, and the business logic should be placed in the service layer for processing as much as possible. It should also be more free in terms of service granularity and service utilization.

Peter_Zhu

Of course it’s not good. The controller layer is only responsible for business data interaction, and the business logic is handled by the service layer

Ty80

The Controller layer of the project I took over now is also extremely large. One method has hundreds of lines, and there are multiple layers of if nesting. I feel that the biggest problem with this is that it will be very troublesome in later maintenance. You need to understand the previous business logic before you can change it; The method that I personally feel is better is controller-service-dao. The service is responsible for the specific logical operations. The three are called in sequence and decoupled from each other as much as possible; the code should be as extensible as possible.

Peter_Zhu
一般controller层=》Service层=》Dao层。
1.controller层,接受请求,进行分页,DTO对象封装操作。
2.service层,执行逻辑,控制并发,事务。
3.Dao层,与数据库交互。
左手右手慢动作

Even if it is placed in service, it will still be a mess. . Still need other methods to avoid complex business situations and poor code maintainability

洪涛

Let a colleague look at the code today. The first thing he said was that you should put the controller code in the service;=_=;
It mainly depends on the complexity of the business. If it is a very simple business, it will not go from the controller to the service. , and then to dao; it also depends on the development specifications of the entire team;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template