3-layer structure development

Source: Internet
Author: User
I, Preface

Recently, several netizens have been discussing hierarchical design in program design, and the response is fierce. Everyone is very interested in this, and the benevolent sees benevolence, the wise sees wisdom. In any case, their views represent their understanding of the program, which is a summary of their practical experience and valuable. Today, we will not comment on whether their opinions are correct or not. Here, I will only talk about my views on layering, hoping to serve as a reference. II, Introduction to L3 architecture development A) What is Layer 3First of all, let's talk about what is a three-tier architecture. The so-called three-tier development is to divide the entire business application into the presentation layer-business logic layer-data access layer-database, and so on. Some of them need to be more detailed, clearly dividing the presentation layer, business logic access, and data access and database access of the client is very conducive to system development, maintenance, deployment and expansion. The software should be layered. In fact, the summary is to achieve "High Cohesion and low coupling ". The idea of "divide and conquer" is used to separate and solve problems, which is easy to control, extend, and allocate resources. Figure 1. layer-3 Presentation Layer: responsible for directly interacting with users, which generally refers to our front-end for data entry and data display. It should not do too much work. It means only work related to the appearance display. He does not care about his work. Business logic layer: used for validation. To better ensure the robustness of the program running. Such as Data Validity judgment. Whether an empty string is input, whether the email is entered, whether the format is correct, and whether the validity of the data type is determined. The string cannot be accepted in an integer, whether database operations are legal, such as determining the validity of the field length. SQL anti-injection, user permission legality judgment, etc., through the above many judgments to determine whether to continue to pass back the operation. Try to ensure the normal operation of the program's data access layer: as the name suggests, it is used to interact with the database. Add, delete, modify, and display data. It should be emphasized that all data objects are referenced only at this layer, such as system. Data. Sqlclient and so on, such applications should not appear anywhere except the data layer. ASP. NET can use the. NET platform to quickly and conveniently deploy a three-tier architecture. The revolutionary change in ASP. NET is that event-based processing is also used on the webpage. You can specify the background code file to be processed, and C #, VB, and J # can be used as the background code language .. Net, and the backend code can conveniently use custom components through naming controls. The display layer is placed on the ASPX page, and the database operations and logic layers are implemented by components, which facilitates the implementation of the three-tier architecture .. B) Why Layer 3So why should we use hierarchical development? What are its unique advantages ?. NET development platform provides us with powerful technical support for development, making our development very convenient and efficient. With the powerful support of code behind, we can effectively separate page design from code design, write code, and design pages simultaneously. This is much faster than the plug-in writing of the old ASP. The HTML is aspx, and the code is Aspx. CS seems quite clear, and there is nothing wrong with it. Indeed, we have done a good job on the basis of functional implementation, especially for a simple application, when the amount of code is not large, such a layer of structure development is enough, and there is no need to make it so complicated. However, this design defect is very serious for a complex large system (we will introduce it in detail below that hierarchical development actually serves large systems ). During the development process, we will constantly copy code everywhere to implement some similar functions. Why do I write the same code so many times? Not only does the program become lengthy, but it is not conducive to maintenance. A small modification may affect many pages. An exception may occur if you are not careful. Make the program abnormal. The most important Object-oriented thinking has not been fully reflected, but it is still a process-oriented method in the guise of object-oriented. Aware of this problem, I began to write some common handlers in the program into a public method encapsulated in the class for other programs to call. Like a collection of functional code and database operations, data operations are reasonably encapsulated like sqlhelper, and SQL statements and parameter lists are used as parameters. During database operations, you only need to input relevant parameters to complete specific data operations. This is the data access layer at the beginning, and no repeated database operation code is required for each database operation. In new application development, the data access layer can be used directly. The encapsulation of one of the three main characteristics of Object-oriented has been well reflected here. It seems that the object-oriented feeling is found that the amount of code is greatly reduced, and the modification is convenient. This should work, right? If the database supplier changes one day, the database access becomes SQL Server due to the increase in data volume? This is troublesome. The original data access layer is invalid, the data operation object is changed, and the data objects on the page need to be modified, because the oledbdatareader object may be used to pass data to the display page, now you have to replace it with the sqldatareader object. The data types supported by SQL and access are also different, data conversion during data display may also need to be modified. The SQL-to-access conversion changes more. Another situation is that, for some purpose, we need to transform a web project into a Windows application. How many modifications are involved at this time? If a lot of processing code is put in your Aspx. CS, or some code is stored in aspx, there may be no aspx in Windows applications. Does the whole system need to be re-executed? This is a fault caused by unreasonable design. In addition, the distributed architecture cannot be implemented in the current design. This means that the above design can only be a small effort at best. I don't know if my explanation shows you some defects in the development mode? Have you ever encountered such a situation? Fortunately, the emergence of multi-layer development architecture effectively solves such problems. By rationally layering the program architecture, the versatility of the program will be greatly improved. In the three layers, the division of labor between each layer is very clear. Is it object-oriented? Like a department in a company, the division of labor between each department is different, in which department the task is completed, the maintenance work of each department is also completed and will not affect other departments. At least, the impact is not great, otherwise, the layer is unreasonable. Efficient system operation is achieved through effective collaboration between layers. The presentation layer is used to accept/display data. It must collaborate with other layers to complete user requests. Too much code should be placed at this layer. The logic layer is used to determine the Data Validity. As we have mentioned earlier, the data layer is used to complete the underlying data interaction. The presentation layer should not implement the functions of the logic layer. Of course, we will make some judgment on the user input on the client side, but the server side should also perform verification. Users can bypass client verification, right? Now let's look at the problem mentioned above. The database has changed. We only need to modify the data access layer. We don't need to worry about it in other places, here, I tend to use custom data entities to take charge of Data Interaction between layers. We fill the data in the Custom entities, for more information about the benefits of using a custom object, see my previous two articles on user-defined entities. The data access layer is used to completely encapsulate the data supplier, so that the data access layer is completely transparent to other layers. In this way, the changes caused by database changes are completely limited to the data access layer. We can use some modes to design a common data access layer, so that even if the database changes, we can easily modify the configuration. It is also easy to change the development platform. Whether it is windows or web, the change is only the interface, that is, the so-called presentation layer. Its kernel is not changed, which is equivalent to making another shell. The presentation layer has very few code, so the modification is very limited. The web program can quickly transition to the Windows program without modifying the other two layers. Have you realized the advantages of Layer 3? Of course, there are still many excellent aspects of multi-layer design. I just introduced a small part of it. Next we will introduce the three-tier concept that I understand. C) My three-tier structure.So how can we write a better three-tier structure? Next, let's talk about the practices I used in programming. Of course, this is just my understanding of Layer 3, which is not necessarily accurate. Welcome to shoot bricks. Haha, the program design pursues the versatility, portability, maintainability, and function expansion of the Code. How can we achieve this? This requires a lot of practical experience. An in-depth understanding of Object-oriented thinking can be achieved. I personally think that a good multi-layer structure design must first be proficient in pattern design, but unfortunately, it has not been able to understand its essence for a long time, the design of the research model is really enjoyable. The above layers are used in sequence design. Web Layer:That is, the presentation layer, which is responsible for responding to user requests. The thinner the layer, the better. Generally, you should not write too much code or do a small amount of code for page display. A large amount of processing work is handed over to other layers. It is like a transmitter who only accepts the request and transmits the request backward. Common Layer:It is used to encapsulate some commonly used functional code, which is mainly used to serve other layers. You can also store a set of custom object types and custom object types. A carrier used for data interaction between different layers. For example, user and usercollection, user-defined entities are not expanded here. If the system is complex, this layer should be thick, including The bll layer below. Bll Layer:The logic layer is used to verify the data validity. Operations on sensitive data must be judged here before determining whether the operation is legal. Dal Layer:The data access layer encapsulates database operations. We can create a common data access layer, which can be used directly at the next development. Very nice. One thing is that the imported and outgoing data are replaced by custom entities, so that the data access layer is completely transparent to other layers. All Database-related code, including SQL statements and stored procedures, is encapsulated here. After the idea of layering is completed, tasks can be divided by layer in the process of multi-person system development. As long as the interface is defined during design, developers can develop at the same time. And there will be no conflicts. The front-end staff simply don't need to know what the database structure is. How to find, update, and delete the data, they can simply call the response method. People at the data access layer do not need to know what is going on at the front end, define interfaces for interaction with other layers, and set parameters. All layers are the same, so you can do your work well, as long as you can be completely transparent to other layers. In this way, the modification can be limited to a relatively small range. But how to organize the specific code at each layer depends on my personal accomplishments. To develop high performance, scalable code should be combined with the idea of pattern design, it can be easily maintained in the future through scientific and rational design of the code structure. III, Summary 1)From the perspective of development and application, the three-tier architecture has a greater advantage than the double or single-tier architecture. The three-layer structure is suitable for group development. Each person can have a different division of labor. collaborative work doubles the efficiency. When developing a double-or single-layer application, each developer should have a deep understanding of the system and have high requirements on capabilities. When developing a three-layer application, you can combine multiple talents, only a few people have a comprehensive understanding of the system, which reduces the difficulty of development to a certain extent. 2)The three-tier architecture can better support the distributed computing environment. Applications on the logic layer can run on multiple machines, making full use of the computing functions of the network. Distributed Computing has a huge potential, far more effective than upgrading the CPU. The Americans used fractional computing to decrypt the password, which is said to have never been broken in just a few months. 3)It is also the biggest advantage of a three-tier architecture is its security. The user can only access the data layer through the logic layer, reducing the entry point and shielding many dangerous system functions.

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.