When we first started to learn about architecture, we first thought of the concept of hierarchy. What is a layer-3 architecture? It includes the presentation layer, business layer, and data access layer. For a novice, the three-layer architecture in the abstract sense is logically divided into three layers.
This is the most basic three-tier architecture model.
Presentation LayerAct as the UI presentation and UI logic of the system. That is to say, the UI (User Interface) belongs to the presentation layer;
For Asp.net webform, people like to write the control logic (Server Control reading, setting, events, and so on) of the UI in the hidden code behind the page and rely on the business logic layer. Of course, the server control supports the data binding function. You can bind the control through the data source. In this way, you can save the code that is hidden behind the scenes.
Therefore, we can divide the presentation layer into the UI user interface and UI logic:
Ui user interface responsibilitiesIt is only used for displaying data input and output.
Ui logic responsibilitiesIt is responsible for data interaction between the business logic layer and UI user interfaces, and makes UI logic independent of UI technology as much as possible.
There are many implementation methods for UI user interfaces, including ASP. NET, winform, WPF, Silverlight, mobile Web, and smart devices.
In the policy that separates the UI page from the UI logic in the presentation layer, the two most commonly used modes are MVC mode and MVP mode.
MVC mode, that is, Model-View-controller mode, Triggers and executes an operation through the view, calls the controller, operates the business layer through the Controller, and finally returns the model for display in the view. The model here can be a domain model (DM) or a data migration object (DTO ).
MVP mode, that is, Model-View-Display ModeUnlike the MVC mode, the MVPs view and model are completely separated. The view defines an interface, and the display device controls the view by calling the interface method. Therefore, views and models are loose. The dashboard also acts as a controller and does not rely on UI technology.
In addition, we will introduce a pattern PM (preentation model), which can be called an MVP variant. in PM, views do not define interfaces. The model here is just a class that represents the view State, the elements in the view are directly bound to the model attributes. For example, in WPF, WPF naturally has a two-way data binding mechanism and an event notification attribute mechanism.
So it is especially suitable for WPF, sliverlight, and so on.
Before starting the business layer, you have to make a premise that in a small project, the presentation layer can directly call the business layer to solve all the problems. However, when the project is large enough to use a variety of forms, such as the use of a variety of UI technology, Asp. net, WPF, and mobile devices, you must consider adding a layer between your presentation layer and the business layer, so that the presentation layer and the business layer can be decoupled, because the business layer is a business middleware platform, it is best not to expose it to the performance layer, which is the legendary service layer. The architecture diagram evolved:
The service layer does not actually perform any specific work. Its function is to organize various business objects. The service layer hides all the details of the business layer from the presentation layer, the server organizes components in the business logic layer and interacts with the presentation layer through the data migration object (DTO). Therefore, a DTO model is generated.
To make the service reusable, you need to use the service interface, and the presentation layer accesses the function through the specified interface. The service implementation inherits the service interface, and the service implementation focuses on calling at the business layer.
For the service layer, common methods include web services,. Net remoting, rest, and WCF technologies.
I suggest using WCF as a service because it is convenient to remotely call the service through configuration.
The service layer eliminates the coupling between the two presentation layers and the business layer. The service layer can implement a remote interface to achieve multi-UI technology or even multi-platform communication.
Of course, adding the service layer also has some disadvantages. If you use the WCF Service, the system call overhead will be increased, thus affecting the performance.
Business LayerIncludes the implementation of the Business Process required by the system and interacts with the data access layer at the lower layer.
We usually call the business layer the business logic layer, but I think the business logic layer belongs to the business layer. The business logic focuses more on the implementation of business logic algorithms. Because the business layer can also include other aspects.
The business layer must include an object model that is fully modeled on the business entity, which expresses all the customer's policies and requirements. Therefore, a domain model is created.
(PS: if you do not use the domain model here, you need to use the business rule layer to verify and control business rules in business functions)
The domain model includes Object attribute definitions, method definitions, and relationships between entities. From this perspective, UML modeling is crucial. By describing UML dynamic and static graphs, You can map them to Domain Models.
The DTO model has just been introduced from the service layer. Here we need a mechanism to convert DTO into a domain model, so a DTO er is generated ).
In addition, the business layer also includes core middleware technologies, including third-party components and workflow engines.
The business layer needs to consider some design patterns for interaction with the data access layer, including the transaction Script Mode, table module mode, activity record mode, and domain model mode.
Transaction Script ModeA process model is used to execute a Business Process. Each method of a transaction script has a specific transaction script, which focuses on a series of sequential operations in the business process, it is easy to implement, but it has a fatal drawback that it will cause a lot of repeated code.
Table module ModeCompared with the transaction script mode, it has a certain structure, and its idea is also very simple. Each data table defines a business component (entity class, entity operation class), in. net uses dataset as the data interaction of the table model. However, it also has a disadvantage that it is driven from the database. It is not suitable for a large number of data tables and complex relationships between data tables.
Activity record modeCan contain data and methods. It is similar to the structure of a data table. Its execution methods can contain crud operations, verification algorithms, and other computing functions. Generally, the domain model is not too complex, and the activity record mode is a good choice. Of course, he also has problems. Similarly, it has a high maintenance cost for complex businesses. If a requirement change leads to database modification, you need to adjust the relevant code in the record object model.
Classic applications: LINQ-TO-SQL and Castle activerecord.
Domain ModelIt is derived from the domain-driven design and is a business-centric design model. It is suitable for complex business logic. The first three methods use the data-driven mode, which features simple features, but when the system reaches a certain scale, it will be difficult to maintain.
Data access layerThe purpose is clear, mainly to provide data persistence functions, including data reading and writing, as well as transaction processing and concurrency control.
There are two ways to operate databases: Orm and ADO. net.
Orm can be implemented using some third-party ORM frameworks, and ADO. Net adopts the database operations that come with ASP. NET.
Different databases have different persistence implementations. Therefore, you can add a storage warehouse interface layer to adapt to different database implementations. Here you can select databases using IOC dependency injection, you can use unity, spring. net and Castle IOC containers.
At last, each layer can depend on the public infrastructure layer.
Public Infrastructure LayerIt can include common modules, logging modules, exception modules, configuration modules, di dependency injection modules, unit test modules, and third-party components (such as nhib.pdf and Sprint. net, Castle, quartz, and so on)
Final diagram:
Summary: The design of the system architecture is affected by the project type, project scale, and business needs. The system architecture is not the same layer. There is no best architecture, but a better architecture, and think about system scalability from the project. The Architecture Analysis in this article is based on the general perspective. In the project, you also need to make adjustments based on the actual situation.