Home > Backend Development > C#.Net Tutorial > Three-tier architecture and pig farming

Three-tier architecture and pig farming

巴扎黑
Release: 2017-04-30 10:26:04
Original
1762 people have browsed it

In order to better allow beginners to get started easily, here we still use an interesting way to talk about some common technical points, dedicated to the rapid improvement of .NET novices! Knowledge is common, the key is the idea of ​​learning. Technology comes from life, and technology can be learned in this way. Just throwing some bricks and mortar.

Hierarchical structure can be seen everywhere in real society. I remember there was a joke about a village head who proudly boasted to his wife: "There are only four people in China who have higher official positions than me, the township head, the county head, the provincial governor and the premier of the State Council." This joke also reflects the phenomenon of stratification in real society. People in society will be stratified, and so will the company's personnel structure. Buildings will be stratified, and even the cages used to make steamed buns will be stratified. Although the purposes of layering vary, they are all created to solve a certain problem. Therefore, the layered architecture is actually a solution created to solve a certain problem.

Commonly used three-tier architecture design

The most commonly used software systems generally talk about the three-tier architecture. In fact, the entire business application is divided into the presentation layer, the business logic layer, the data access layer, etc. Some are even more detailed. By decomposing the business details, different functional codes Decentralization is more conducive to system design and development, and at the same time provides smaller units for possible changes, which is very conducive to system maintenance and expansion.

The common three-tier architecture basically includes the following parts, as shown in Figure 1.


Figure 1 Common three-tier architecture

  • Data Access Layer DAL: Used to implement interaction and access with the database, to obtain data from the database or to save data to the database.


  • Business Logic Layer BLL: The business logic layer connects the upper and lower layers and is used to logically process the upper and lower interactive data to achieve business goals.


  • Presentation layer Web: Mainly implements interaction with users, receives user requests or returns the display of data results requested by users, and the specific data processing is left to the business logic layer and data access layer.

In many cases of daily development, in order to reuse some common things, some things used by each layer will be abstracted. For example, we separate data object entities and methods in order to pass them through multiple layers, such as called Model. Some common general auxiliary classes and tool methods, such as data verification, caching processing, encryption and decryption processing, etc., are also separated separately and used as independent modules in order to allow reuse between various layers, such as called Common.

At this point, the three-tier architecture will evolve into the situation shown in Figure 2.


Figure 2 Three-tier architecture evolution results

  • Business Entity Model: Used to encapsulate entity class data structures. It is generally used to map data tables or views of the database to describe objectively existing objects in the business. The model is separated for better decoupling, better play to the role of layering, better reuse and expansion, and enhanced flexibility.


  • Common class libraryCommon: Common auxiliary tool class.

We can abstractly encapsulate the common operations of the database into data operation classes (such as DbHelperSQL) for better reuse and simpler code. The bottom layer of the data layer uses general database operation classes to access the database. The final complete three-layer architecture is shown in Figure 3.


Figure 3 The final complete three-tier architecture

Database access class is an encapsulation of ADO.NET, encapsulating some commonly used repeated database operations. For example, Microsoft's enterprise library SQLHelper.cs, Dongsoft's DBUtility/DbHelperSQL, etc. provide DAL with auxiliary tool classes for accessing databases.

Through the above analysis, we know what the three-tier architecture commonly used today looks like. At the same time, we also know some of the evolution processes of the three-tier architecture during its use. So, why is it layered in this way, and what is the role of each layer of structure? Let's continue reading.

Interesting understanding: three-tier architecture and pig farming

The price of pork has been soaring. It is said that some people have become rich and well-off by raising pigs. Programmers say that writing code has no future and they might as well raise pigs. However, don’t think that raising pigs has no technical content and is easier than writing code. In fact, raising pigs is not as technical as writing code. Pigs are also very knowledgeable. In order to better understand the three-tier architecture, let's take pig farming as an example. As the saying goes: "If you haven't eaten pork, you haven't seen a pig running!".

Figure 4 is an interesting diagram of the pig industry assembly line with a three-layer architecture.


Figure 4 Three-layer structure and pig farming

Comparing Figure 3 and Figure 4, we can see:

  • The database is like a pig pen. All pigs are stored in different pig pens in an orderly manner by area or number.


  • DAL is like a slaughterhouse. The pigs are taken out from the pig pen and slaughtered (processed). The corresponding parts (fields) are taken out as required, or classified and organized (statistics) to form a whole box of pork ( Data set), transmitted to the food processing plant (BLL). Originally, the same group of people were in charge of both catching and killing pigs here. Later, I felt that the efficiency was too low, so I let some people come out to be responsible for catching pigs (DBUtility), and catch designated pigs according to requirements.


  • BLL is like a food processing factory, which deeply processes pork into various edible foods (business processing).


  • Web is like a shopping mall, which packages food into beautiful products that can be sold and displays them to customers (UI presentation layer).


  • Pork is like Model. No matter which factory (level) it is, the essence delivered in every link is pork, and pork runs through the entire process.


  • General class library Common is equivalent to various tools used by workers, providing common tools (classes) such as butcher knives, ropes, scissors, packaging boxes, tool carts, etc. for each factory (level). In fact, each department could have made its own tools, but that would have been less efficient and unprofessional, and a lot of work would have been repeated. Therefore, someone specially opened such a factory to make these tools and provide them to various factories. With such a division of labor, the factories could concentrate on their own business.

Of course, this is just a metaphor. The purpose is to let everyone better understand. The actual situation will be different in details. This example only illustrates the one-way process from the pigsty to the shopping mall. In actual three-layer development, data interaction is two-way and can be retrieved or stored. However, it is said that there is a machine that drives the pigs in from one end, and ham will pop out from the other end. If the ham sausage cannot be sold, put it in again from the other end, and the pig will come out exactly as it is. It is a sci-fi machine. I didn't expect it to be connected to the three-layer structure. The above is just a joke, but it also makes the basic concept of the three-tier architecture easier to understand.

Having said so much above, some people may ask, can’t I directly retrieve the content from the database and operate it directly? Why go to so much trouble to use a three-tier architecture? What are the benefits of the three-tier architecture?

No stratification, of course, just like the whole process is not divided into slaughterhouses, processing plants, etc., all the work (slaughtering, processing, and sales) is completed in the same place (factory). But why processing plants and shopping malls? Because when the scale is relatively large, management will become very complicated, and this kind of breeding method can no longer meet the needs of scale. Moreover, from the perspective of social development, social division of labor is a manifestation of human progress. The advantage of social division of labor is that it allows suitable people to do what they are good at, greatly shortening the average social labor time and significantly improving production efficiency. Only those who can provide high-quality and efficient labor products can obtain high profits and high value in market competition. The most profound meaning of making the best use of people's talents and making the best use of materials comes from the division of labor in society. The same goes for software development. When working on small projects, there is really no difference between layering or not, and it seems more troublesome and verbose. But when projects become larger and more complex, layering shows its advantages. Therefore, whether to divide into layers or not depends on the actual situation of the project and cannot be generalized.

Related documents: Layered development ideas and Xiaolongbao

The above is the detailed content of Three-tier architecture and pig farming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template