Database encapsulation Design

Source: Internet
Author: User
When looking at another program, I found that all the methods of the mysql operation class encapsulated by him are static, while the methods of the encapsulated redis class are not static, is there any special significance or consideration for this design? When looking at another program, I found that all the methods of the mysql operation class encapsulated by him are static, while the methods of the encapsulated redis class are not static, is there any special significance or consideration for this design?

Reply content:

When looking at another program, I found that all the methods of the mysql operation class encapsulated by him are static, while the methods of the encapsulated redis class are not static, is there any special significance or consideration for this design?

No way to evaluate it. His choice may be suitable for scenarios where his code is used, such as limited database objects, and more than 80% of requests need to operate on the database, in this way, the database connection can be started during the process initialization, but redis is rarely used, and there are multiple redis objects, in this case, it will be a waste of constructing all redis during initialization. So he made this choice.

We suggest you learn to think and learn.
1-when the author writes the code, others can only guess, not all;
2-do not think that you cannot write it out. If someone else writes it out, what others write is absolutely correct and excellent;
3-answer questions by yourself first, and then refer to others' answers. Do not take precedence over others' ideas;
4-The larger the project, the higher the chance of code corruption, and the worse the project, because it is impossible for one person to complete all development, and even a person to develop has a peak and a low point, the design idea is good, it is common for specific programming to fail to meet expectations for various reasons.

Back to the problem itself, from my personal experience (and many frameworks), static methods are usually tool methods, such as DB: factory, App: getModel, Date: format, database Operations and redis operations are obviously not tool methods, so static operations are not recommended.
All mysql methods are static, which is actually a failure in the general direction. Developers are unlikely to check the database connection status in the DB: insert method. Therefore, they must have already connected to the database. On the one hand, if the current process does not require database intervention, this connection will be wasted, on the other hand, this obviously prolongs the database connection time, that is, if it is late binding, this database connection may last for only 0.05 s. Due to pre-binding, the database connection actually lasted 0.1 s, in addition, 0.05s is idle, waiting for the php process to process other logic, both of which increase the pressure on the database.

-- Supplement --
If the database: insert contains database connection status detection, on the one hand, additional logic is added, and on the other hand, it is not suitable for multi-database projects (if you want to be compatible with multiple databases, therefore, it is troublesome to allow the input of database connection parameters when the insert operation is called)

Your opinion is for reference only.

First, the static execution efficiency is high, because the script is loaded into the memory during running, and the new requires the instantiation process. Then, mysql is complicated because the SQL statement is compared with the redis operation method, the mysql encapsulation method may be complicated, while the redis operation statement is relatively simple, and the author may be too lazy to perform complicated encapsulation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.