关于业务分层
业务分层思想
根据MVC分层思想,我们的项目都会分成3层,而业务层是在M里面的。
现在的项目都会把M分层两层即DAO
层(持久层)和Service
层(业务层)。
由于业务的发展,Service
层里面的业务会越来越复杂,这个时候为了保持Service
层方法的抽象层次一致性,一般我们都会写成堆成堆的private
方法,举个例子。
<code> public class OrderServiceImpl implements OrderService{ public void createOrder(){ checkActivity(); createOrderInfo(); createTrade(); } private void createOrderInfo(){ createOrderInfo(); createSkuInfo(); } private void createOrderInfo(){ //创建订单信息 } private void createSkuInfo(){ //创建订单对应单个SKU信息 } private void createTrade(){ //调用支付接口,创建支付信息 } private void checkActivity(){ //调用活动接口,看该订单对应的商品是否有活动信息 } } </code>
createOrder()
方法中保持了抽象的一致性(增加代码的可阅读性,方便维护),但是里面的私有方法createOrderInfo()
里面里面又有一个私有方法。如果这种情况很多的话,那么这个类就会变成一个私有方法很多的类。这个时候我觉得应该在service
和dao
中再增加一层,该层的抽象层次在service
和dao
之间。这样子的话,我们就可以把一些业务封装在该层,增加代码的可读性和维护性至于扩展性我目前还没有想到哪个场景可以增加扩展性。
不知道你们对这个是怎么看的。
回复内容:
业务分层思想
根据MVC分层思想,我们的项目都会分成3层,而业务层是在M里面的。
现在的项目都会把M分层两层即DAO
层(持久层)和Service
层(业务层)。
由于业务的发展,Service
层里面的业务会越来越复杂,这个时候为了保持Service
层方法的抽象层次一致性,一般我们都会写成堆成堆的private
方法,举个例子。
<code> public class OrderServiceImpl implements OrderService{ public void createOrder(){ checkActivity(); createOrderInfo(); createTrade(); } private void createOrderInfo(){ createOrderInfo(); createSkuInfo(); } private void createOrderInfo(){ //创建订单信息 } private void createSkuInfo(){ //创建订单对应单个SKU信息 } private void createTrade(){ //调用支付接口,创建支付信息 } private void checkActivity(){ //调用活动接口,看该订单对应的商品是否有活动信息 } } </code>
createOrder()
方法中保持了抽象的一致性(增加代码的可阅读性,方便维护),但是里面的私有方法createOrderInfo()
里面里面又有一个私有方法。如果这种情况很多的话,那么这个类就会变成一个私有方法很多的类。这个时候我觉得应该在service
和dao
中再增加一层,该层的抽象层次在service
和dao
之间。这样子的话,我们就可以把一些业务封装在该层,增加代码的可读性和维护性至于扩展性我目前还没有想到哪个场景可以增加扩展性。
不知道你们对这个是怎么看的。
1.个人认为,MVC里面,control,service,dao都属于控制层。前端界面作于view,数据库充当Model的角色
2.在service里面有很多private方法其实没有太大问题。如果需要可以增加manager这样的层次对某些privae方法进行封装。但是层次分的太多,在实际开发的时候就会觉得很繁琐,一个小功能就需要来几个层次间串。其实也是一种维护和开发成本。
3. 一般事务的界限会划分在service层的public方法,所以写private方法的时候要小心,不然嵌套在一起会出现一些问题。
- 同意 闲读岁月 的观点: MVC里面,control,service,dao都属于控制层。前端界面作于view,数据库充当Model的角色
- 如果service里的private方法都只有在这个service中存在,即其他service不会也有代码相同或类似的private方法,那我感觉写private方法没有什么问题,如果其他service中也有和这个service中private方法相同的代码,可以考虑单独抽出一层。
service里的每个方法只完成一个业务逻辑,如果业务逻辑比较复杂,可以考虑拆分成一个helper或者manager来专门处理这个复杂的逻辑,service就清清爽爽了,而不用一大堆的private方法在service里了。
service建议做成一个facade,仅仅是其他业务操作的组合,控制事务用的。业务逻辑实现可以放到domain或者command里头。
个人感觉再拆分一层没有必要,写个helper类就差不多了,一个service的代码过长可以多拆分几个service,比如说orderservice可以拆分成ordermakeservice,ordercancelservice。平行拆分和再加一层拆分比较起来,平行拆分更好理解业务。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
