Application Framework practice 5: Composition of the application framework

Source: Internet
Author: User

The application framework is used to support application development. It is like a nanny who cares for programmers in every detail of development. What should the application framework contain? In principle, everything you want to reuse can go into your framework.

A major difference between a novice developer and an experienced veteran is that the method for reusing code is different. Developers prefer copying and pasting, while veterans prefer abstraction and encapsulation. I have heard people say that copying code is a devil. Why is it so serious?

When you copy and paste the code around and suddenly realize that you need to modify it, the tragedy will happen. You need to find all the code and change it one by one. This is a kind of physical activity, if there are a lot of pasting points, such as 100, it will take a while. However, this is not serious. The more serious problem is that there may be a fish that may be missed. If you miss a place, you will not be able to find it on the spot, because it is unlikely that you will re-check the code again, let alone test all the places. The next mine will be buried, and we will see when we step on it, but it will not be found in the short term. Maybe three months later, someone found some strange bugs and submitted them to you, so you opened Vs and kept looking for errors in breakpoint debugging. After a lot of time, you finally found them, then you smiled and told yourself, "nothing. I missed a place last time and didn't change it. It's just a small bug ". This is like a financial business. It only misplaces the decimal point. It is nothing for him. It is not a technical problem, but the result is very serious.

If you are often confused by copying and pasting, and want to improve the method, you are looking for a solution. Two solutions are generally required for copying and pasting. One is abstraction and encapsulation, and the other is code generated based on the template.

What code is worth encapsulating?

First, consider the technical aspects, such as file upload and download. Why do you want to encapsulate these things? Although the code is not complex, do you remember it at any time? Isn't it tiring to knock on so many codes, the key point is, do you need to copy and paste it. After the technical code is encapsulated, there is a well-known term-Public operation class. Generally, a helper is added to the class name to indicate the help class. After a large number of public operation classes are formed, the development process will be greatly simplified. Its goal is to help you get rid of Technical troubles and put more energy into your business.

Second, consider how to enhance existing systems, such as providing easier-to-use APIs through extension methods. In addition, it is necessary to provide system robustness support, such as log tracking, global exception capturing, dependency injection, and security mechanisms.

Third, consider which codes are available for each business class and abstract them to the base class. If you use the DDD architecture, create a base class based on the DDD layered architecture and build blocks.

Fourth, the presentation layer support can encapsulate elements on the interface to provide more unique functions. For example, if a button requires the permission to be displayed, you need to write a judgment in the presentation layer. If the button has no permission, the control will be hidden. If there are similar requirements in many places, you will find that the presentation layer is filled with a lot of judgments. Encapsulate these unique functions into controls to get clearer code.

Fifth, let's see what types of services are commonly used, such as addresses, including country, province, city, district/county, street, postal code, and other information. Even if you change the project, the address is still the content. In addition, many projects can benefit from extracting and encapsulating industry-specific things.

6. encapsulate third-party business systems, such as third-party application platforms and APIs provided by hardware devices. After encapsulation, use third-party interfaces, just like calling local APIs.

7. Encapsulating some universal modules can significantly improve the progress, such as permission management, Department management, and employee management.

After abstraction and encapsulation at all levels, you will find that there are still a considerable number of codes that need to be copied and pasted back and forth. observe these codes carefully and their structures are similar, it may be because the database fields are different, so you need to change it. The most common is the CRUD operation. Since the information system is ultimately a CRUD operation, almost every database table has corresponding operations. But it's still an old problem. The copy and paste method is not safe and inefficient. Especially in a layered architecture, the problem is even more serious because the code has to go beyond many layers. A better way is to create a template. Using a code generator to create basic code is not only fast, but also quality assurance.

Next, we will summarize the scope of the application framework:

  • Public operation class (helper, third-party technical framework encapsulation)
  • System Function enhancement and expansion
  • Layer supertype (technology base class)
  • Presentation Layer Control Encapsulation
  • General Business class and business base class Encapsulation
  • Third-party business interface Encapsulation
  • General module Encapsulation
  • Supporting Code Generator

In short, all reusable items are put in.

Application Framework practice 5: Composition of the application framework

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.