Application Framework Practice 7: layered architecture Selection

Source: Internet
Author: User

To build an application framework, you must first consider which layered architecture you are going to adopt, and then determine the VS solution and assembly to be created based on the logical hierarchy of the application framework.

If the project is small, the demand is simple, the time is too short, and you have not accumulated any experience, then the single-layer architecture will be the first choice, as shown in the simplest single-layer architecture (to focus your attention, I deleted all irrelevant files ).

 

The main advantage of a single-layer architecture is that the Code is concentrated and straightforward. Unlike the multi-layer architecture, each operation needs to be transferred layer by layer. For user. aspx, all relevant code is directly written to the ASPX page or the post code, including control operations, business operations, and database operations on the interface.

In addition, the single-layer architecture is quite in line with the habits of beginners. Beginners are still not familiar with programming syntax and. Net basic APIs. Which of the following will give too much consideration to the code structure.

The use of a single-layer architecture mainly depends on. net powerful control system, drag controls, configure attributes, write event processing functions, is a true portrayal of single-layer architecture programming, this is also. net programmers have always been impressed by others.

For small projects that mainly contain crud operations, the single-layer architecture can work well, but if the project has some complicated business logic, such as the order process, the code will soon become a mess of mud, lost direction. The business logic is mixed with the presentation layer code. The result is that any modifications to the interface may cause the business logic to fail.

To improve code reuse, you can extract operations that need to be processed on each page, such as permission check, log tracking, and global error processing, to a base class of pagebase, in the base folder, you can create a helpers folder to place the public operation class.

The accumulation of a single-layer architecture for the next similar project is minimal, and most of the Business Code will be discarded. It may be a technological change. For example, if you used web form before, you now need to use win form, because the Business Code is highly coupled with the presentation layer code, it is obvious that the previous Code cannot be used. Even if the technology remains unchanged, it will be difficult to add and modify new features in the mud previously developed. Some logic seems very simple. When you do it, you find that it is not so simple. Modifying one place will affect more places.

The technical accumulation is mainly used by copying files for the next project. A better way is to extract the technical accumulation separately into A. Net class library, as shown in.

 

In this way, the separation of business and technology is realized. When the next project arrives, you only need to introduce util. dll.

To explore the limits of a single-layer architecture, you need to develop a set of powerful custom controls to encapsulate all the database operations, verification operations, permission operations, and other content, and set database column names on the controls, SQL statements are directly generated and sent to the database without passing through the able or object. With the code generator, simple projects can be developed at an astonishing speed.

No matter how you try to implement a single-layer architecture, you can't do anything about complex projects. You can use a single-layer architecture to develop complex projects with complex business logic, and what you get is messy. Improving the code quality can alleviate these problems to some extent, but to fundamentally solve them, we must move closer to the multi-layer architecture.

From the above analysis, we can see that the single-layer architecture is not competent in complex business fields, and its reuse capability is very limited. Because the application framework aims to improve code reuse in both technology and business, single-layer architecture is not a proper choice.

Basically, the information system operates databases and provides a set of Operation interfaces. If it is divided into different layers according to relevant responsibilities, it will help improve project management and Code definition. Layered Architecture is an application of SRP (single responsibility principle). It aggregates highly relevant things and connects each layer with interfaces, in this way, a high cohesion and low coupling design can be achieved.

Currently,. Net programmers generally use a traditional three-tier architecture. The architecture is as follows.

 

The core of the traditional three-tier architecture lies in the business logic layer. The presentation layer places data in the entity and passes the data as parameters to the business logic layer. The business logic layer uses procedural code to process the business, to access the database, call the data access layer.

The traditional three-tier architecture separates performance, business, and data access to different layers. When switching the performance Layer technology or changing the database, you only need to replace the relevant components and do not need to rewrite them all.

There is no white lunch in the world. In addition to the benefits of clarity, reusability, and scalability, there are also some problems, mainly due to reduced intuition, increased complexity, and increased workload. To implement a function in a single-layer architecture, you may need only one method to complete it and it is easy to understand. After Entering the three-layer architecture, each operation is separated and dispersed into different layers. Each layer has a part of the code and needs to jump back and forth in several layers to get a glimpse of the whole picture, so the intuition is reduced. In addition, any operation needs to be written in multiple places, increasing the workload.

If you write code strictly in accordance with the requirements of the three-tier architecture, do not perform business operations on the presentation layer, or data operations on the performance layer. The business layer only needs the logic to achieve high maintainability. However, many beginners do not know the role and responsibilities of the architecture. They will randomly find a place to place the code, which is mostly the performance Layer. As a result, not only the code quality is poor, but the architecture is still very complex, the loss is worth the candle.

The traditional three-tier architecture is particularly controversial in terms of the model entity layer. On the surface, these entities seem to be business objects, such as customer. After carefully observing this class, we find that it is full of attributes and there is no way to use it as a data container to transmit data between layers.

Some people always think that they are engaged in object-oriented development. After all, C # is an object-oriented language and uses a three-tier architecture. It also creates entities. Isn't it object-oriented development? In fact, the traditional three-tier architecture is still process-oriented, but it is covered with an object-oriented coat. Some people are still dismissive about whether or not it is object-oriented development. "Let's take care of these theories, but we only do what we do, however, if you want to pursue higher encapsulation, reusability, and maintainability, you must go deep into the object-oriented approach.

The core of object-oriented modeling is to build models based on concepts. For example, although the traditional three-tier architecture does create a customer class in the model layer, the model layer is a secondary component in the three-tier architecture, is a dispensable thing. It is also feasible to replace the customer class with a able.

The entity in the model is not very useful because it violates the characteristics of the object. The real object is a collection of data and operations. Some people refer to the simple data object as the anemia model, and the object with data and rich operations as the congestion model, which means that only the attribute data objects are inherently inadequate, malnutrition, and low IQ, it only eats (data) and does not work.

Why must data and operations be put together for an object to exert its power? Because the data is closely related to operations on the data and changes at the same time. Encapsulating data and operations together can provide unique access points for operations. The main advantage is centralized management of operations to eliminate code redundancy. When data changes, most of the related operations need to be modified at the same time. Because the Code does not have redundant copies and the modifications are completed within the object, the maintainability of the Code is greatly improved without affecting the outside world.

After the data and operations of business objects are separated, the first impact is that the business logic is scattered and difficult to manage. Since operations do not have a uniform location, no one knows where these operations are located? It may be at the business layer, performance layer, or stored procedure. Even if you write code strictly in accordance with the responsibilities of the three-tier architecture, all business logic is in the business layer, but many classes may use this entity. In which class is an operation? This problem is even more obvious when the team is fighting. If he cannot easily find the method he wants, he will add one by himself, leading to redundancy. The second impact is obvious code redundancy, and code redundancy is the natural enemy of maintainability. Whenever data changes or bugs are detected, you need to find copies of all the operation codes for modification. If any omission occurs, the mines will be laid.

Of course, crud simple operations are not covered in the above discussion. The above mainly refers to the extent to which the business logic is affected by data.

We can see that the three-tier architecture has made great progress compared to the single-tier architecture, but it still has some defects that can be improved. If we merge the model entity layer and BLL business logic layer, the object-oriented model is called the domain model in enterprise application architecture model. However, Eric Evans guides how to use object-oriented development in a more specific way in the domain-driven design.

Should the congestion model (Object-Oriented) or anemia model (process-oriented) be used )? Do You Want To Use ORM or DDD )? These issues are always controversial. However, my suggestion is that if you are still preparing to mix in the. NET community for several years, it is better to do so early, because this is the trend of the times, as if a single-layer architecture is eventually replaced by a three-tier architecture. In addition, if the domain-driven design is not used well, it will be similar to the three-tier architecture at best, so you do not need to worry about it.

This series of articles will use the Entity Framework and DDD layered architecture to demonstrate the construction of the application framework, so this article will not introduce DDD.

This article briefly introduces the evolution process of the layered architecture and provides some reference for you to select the appropriate architecture. The final conclusion is that whether you are willing or not, due to the development of the software industry, in particular, with the help of Microsoft technology, you will always embark on the object-oriented path. Instead of being passive, it is better to take the initiative to learn.

Some of my friends found that this series of real names were all nonsense and there was no code. Some of them liked to collect the source code and wait for a dry job. What I want to say is that this practical series introduces the architecture and framework after all. If you only get a few lines of code and do not really understand how to build an application framework for your project, the Code cannot form an architecture, or is it useless. In addition, the code I provide is also collected and organized around, there is nothing special, if you need the source code, Baidu will be much more, but I hope you can understand every detail, this is more helpful.

  . NET application framework exchange QQ group: 386092459. You are welcome to join the discussion..

Application Framework Practice 7: layered architecture Selection

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.