How to Design the architecture?

Source: Internet
Author: User
Tags in domain
Part 1 Layer
The concept of layer is very difficult in the computer field. The computer itself embodies the concept of a layer: the system call layer, the device driver layer, the operating system layer, and the CPU instruction set. Each layer is responsible for its own responsibilities. The Network is also a layer concept, the most famous OSI Layer-7 protocol.

It is also easy to use in the software field. Why? Let's take a look at the advantages of the layer-based technology:

● You use a layer, but you do not need to understand the implementation details of the layer.
● You can use another technology to change the underlying layer without affecting the applications of the above layer.
● Reduces dependencies between different layers.
● Easy to develop exit layer standards.
● The bottom layer can be used to create multiple services on the top layer. Of course, the layer also has vulnerabilities:

● The layer cannot encapsulate all functions. Once a function change occurs, all layers are bound to be affected.
● Reduced efficiency.

Of course, the most difficult problem at the layer is what each layer has and what responsibilities it has to bear.

Typical three-tier structure

The three-tier structure is probably quite familiar to everyone. Is the presentation layer, domain layer, and infrastructure layer.

The presentation layer logic mainly processes the interaction between users and software. Currently, wimp and HTML-based interfaces are the most popular. The main responsibilities of the presentation layer are to provide users with information and translate users' instructions. To the business layer and infrastructure layer. The logic at the infrastructure layer includes communication between processing and other systems, representing the execution of tasks by the system. For example, database system interaction and interaction with other application systems. For most information systems, the greatest logic at this layer is to store persistent data.

Another is the domain-layer logic, which is also called the business logic. It includes the calculation of input and storage data. Verify the data from the presentation layer and assign an infrastructure layer logic according to the instructions of the presentation layer.

In domain logic, people are always confused about what is domain logic and what is other logic. For example, there is a logic in a sales system: if the sales volume increases by 10% this month from the previous month, it will be marked in red. To implement this function, you may put the logic in the presentation layer and compare the numbers for two months. If the number exceeds 10%, it will be marked as red.

In this way, you put the domain logic in the presentation layer. To separate these two layers, you should now provide a method in the domain layer to compare the growth of sales numbers. This method compares the numbers of two months and returns the boolean type. The presentation layer simply calls this method. If true is returned, it is marked in red.

Example

The layer technology does not have any permanent skills. How to use it depends on the specific situation. The following three examples are provided:

Example 1: an e-commerce system. It is required to be able to process a large number of user requests at the same time. The user scope is global, and the number is still growing. However, the domain logic is simple. It is nothing more than the order processing and the connection to the inventory system. This requires us 1. The presentation layer should be friendly and can adapt to the widest range of users, so we adopt the HTML technology; 2. Support distributed processing for thousands of accesses at the same time; 3. Consider future upgrades.

Example 2: A leasing system. The system has fewer users, but the domain logic is very complicated. This requires us to create a system with extremely complex domain logic, and provide a convenient input interface for their users. In this way, wimp is a good choice.

Example 3: a simple system. Very simple, with few users and few logic. But there is no problem. It simply means fast delivery and future upgrades should be fully considered. Because the demand is constantly increasing.

When to Layer

Such three examples require that we cannot solve the problem in an overall manner, but should formulate specific solutions based on the specific circumstances of the problem. These three examples are typical.

In the second example, you may need to strictly divide it into three layers and add another mediating layer. Example 3 is not required. If you only want to view data, you only need several server pages to place all the logic.

I usually separate the presentation layer from the domain layer/infrastructure layer. Unless the Domain Layer/infrastructure layer is very simple, and I can use tools to easily bind these layers. The best example of this two-layer architecture is that in the VB and Pb environments, it is easy to build a Windows interface system based on SQL database. This presentation layer is very consistent with the infrastructure layer, but once verification and computing become complex, this method has inherent defects.

Most of the time, the domain layer and the infrastructure layer look very similar. At this time, they can actually be put together. However, when the business logic at the domain layer is different from that at the infrastructure layer, you need to separate them.

More layer Modes

The three-tier architecture is the most common, especially for is systems. Other architectures also exist, but they are not suitable for any situation.

The first is brown model [Brown et al]. It has five layers: presentation layer (presentation), control/mediation layer (Controller/mediator), Domain Layer (domain), data mapping layer (data mapping ), and data source ). In fact, it adds two intermediate layers in the three-tier architecture. The control/mediation layer is located between the presentation layer and the domain layer, and the data ing layer is located between the domain layer and the infrastructure layer.

The presentation layer and the mediation layer of the domain layer, which are commonly called the representation-domain mediation layer, are a common layered method, usually for some non-visual controls. For example, organize the information format for a specific presentation layer, navigate between different windows, process the transaction boundary, and provide the server facade interface (for the specific implementation principle, see the design mode ). The biggest danger is that some domain logic is put into this layer, affecting other presentation layers.

I often find it advantageous to allocate behavior to the presentation layer. This simplifies the problem. However, the presentation layer model is complicated. Therefore, it is worthwhile to put these behaviors into non-visualized objects and extract a representation-domain intermediary layer.

Brown Isa
Presentation Layer
Control/mediation layer representation-domain mediation Layer
Domain Layer
Database er in database interaction mode at the data ing Layer
Data Source layer infrastructure layer

The mediation layer between the domain layer and the infrastructure layer belongs to the database mapper mode mentioned in this book and is one of the three methods for connecting the domain layer to data. And representation-domain intermediary layer at a glance, sometimes useful, but not always useful.

Another good layered architecture is the J2EE architecture. For more information, see the book "J2EE Core Model. Its hierarchy is client, presentation, business, integration, and resource ). Differences include:

J2EE Core Isa
Presentation Layer on the client
Presentation Layer running on the server
Business Layer Domain Layer
Integration layer infrastructure layer
External data for communication at the resource layer infrastructure layer

Microsoft's DNA Architecture defines three layers: presentation layer, business layer, and data access. This is similar to my architecture, however, there are still many differences in data transmission methods. In Microsoft's DNA, operations at each layer are based on the SQL query result set sent from the data storage layer. In this way, the coupling between the presentation layer and the business layer and the data storage layer is increased. The action of the DNA record set between layers is similar to that of the data transfer object.

Part 2 Organization domain Logic

To organize layer-based systems, the first thing is how to organize domain logic. There are several ways to organize domain logic. But the most important method is the transation script and domain model. One of them is selected, and others are easy to decide. However, there is no obvious line between the two. Therefore, it is also a university question how to choose. In general, we think domain model can be used for systems with complicated domain logic.

Transation script is used to process user input in the presentation layer.Program. This includes verification, computing, storage, calling other system operations, and returning data to the presentation layer. A user's action represents a program. This program can be a script, transation, or subprograms. In example 1, check that adding a book to the shopping cart and displaying the delivery status can all be a transation script.

Domain model is a model for domain nouns, such as books and shopping carts in Example 1. Testing, computing, and other processing are all put into the domain model.

Transation script is structured thinking, and domain model is OO thinking. Domain model is difficult to use. Once you get used to it, you can organize more complex logic and your thoughts will be more oo. At that time, even a small system will naturally use the domain model.

But how to choose? If the logic is complex, use domain model: If you only need to access the database, the transation script will be better. However, the demand is constantly evolving, and it is difficult for you to ensure that future requirements will be so simple. If your team is not good at using the domain model, you need to weigh the input-output ratio. In addition, even the transation script can separate the logic from the infrastructure. You can use gateway.

For example 2, domain model is undoubtedly used. You need to weigh the value of instance 1. For example 3, it is hard to say whether it will be like example 2 in the future. You can use transation script now, but you may need to use domain model in the future. Therefore, architecture decisions are crucial.

In addition to the two models, there are other moderate models. Use Case Controller is between the two. Only the business logic related to a single use case is put into the object. Therefore, they are still using the transation script, while the domain model is only a set of database gateways. I don't quite use this mode.

Table module is another moderate mode. Many GUI environments rely on the returned results of SQL queries. You can create objects in the memory to split the GUI from the database. Write a module for each table. Therefore, each row requires a keyword variable to identify each instance.

The table module is suitable for scenarios where many components are built on a general relational database and the domain logic is not complex. Microsoft COM and Its. NET environment with ADO. NET are suitable for this mode. For Java, it is not applicable.

One problem with domain logic is that domain objects are very bloated. Because the object has too many behaviors, the class is too large. It must be a superset. In this case, we need to consider which behaviors are generic and which are not. They can be processed by other classes, either Use Case Controller or presentation layer.

Another problem is replication. It will lead to complexity and inconsistency. This is more dangerous than being bloated. Therefore, we would rather be bloated than copying. Wait until the system is bloated and try again.

Select a region to run the domain Logic

Our focus is on the logic layer. The domain logic either runs on the client or the server.

The simple method is to concentrate on the server. In this way, you need to use the HTML front-end and web server. The advantage of doing so is that the upgrade and maintenance are very simple. You do not need to consider the synchronization between the desktop platform and the server, or the compatibility of other software on the desktop platform.

Running on the client is suitable for situations where fast response is required and no network connection is available. In the server logic, a user's small request also needs information to be circled from the client to the server. The response speed must be slow. Besides, the network coverage does not mean that it reaches 100%.

What about each layer?

Infrastructure layer: It is generally on the server, but sometimes it will also copy data to a suitable high-performance desktop, but this is the issue of synchronization.

Where the presentation layer runs depends on the design of the user interface. A Windows interface can only run on the client. A Web interface is run on the server. There are also special examples of running Web server on a desktop, such as X server. However, this situation is rare.

In example 1, there are no more options, but only on the server side. Therefore, every bit of yours will go around a large circle. To improve efficiency, try to use some pure HTML scripts.

The main reason for choosing a Windows interface is to execute complicated tasks and a suitable application, while web GUI is not competent. This is the practice of Example 2. However, people should gradually adapt to the Web GUI, and the Web GUI functions will become more and more powerful.

The rest is the domain logic. You can either put all the servers, or all the clients, or both sides.

If it is on the client side, you can consider putting all the logic on the client side, so that at least all the logic is in one place. However, moving the Web server to the client can solve the problem of no network connection, but it will not be of much help to the response time. You can still separate the logic from the presentation layer. Of course, you need additional upgrade and maintenance work.

Logic on both the client and server is not a good solution. However, it is applicable to situations where only some domain logic is available. There is a trick to encapsulate the logic that is unrelated to the rest of the system. Domain logic interface

Your server has some domain logic and needs to communicate with the client. What interfaces should you have? Either an HTTP interface or an OO interface.

The HTTP interface is applicable to Web browser, that is, you need to select an HTML presentation layer. The latest new technology is Web Service, which communicates through HTTP, especially XML. XML has several advantages: a large amount of communication, a good structure, and only one loop. This reduces the overhead of remote calls. XML is also a standard that supports heterogeneous platforms. XML is text-based and can be passed through the firewall.

Although XML has so many advantages, an OO interface still has its value. The hhtp interface is not obvious and it is difficult to see how the data is processed. The method of the OO interface has variables and names, so it is easy to see the process of processing. Of course, it cannot pass through the firewall, but it can provide control such as security and transactions.

The best thing is to take the strengths of both. The oo interface is down, and the HTTP interface is on. However, this will make the implementation mechanism very complicated.

Part 3 organize Web Server

Many people who use HTML cannot really understand the advantages of this method. We have a variety of useful tools, but it is difficult to maintain the program.

There are two ways to organize programs on the Web server: script and Server Page.

A script is a program that uses functions and methods to process HTTP calls. For example, CGI scripts and Java Servlets. It is no different from a common program. It obtains HTML string data from the web page, and sometimes requires expression matching. This is why Perl can become a common language for CGI scripts. While Java servelet leaves this analysis to the programmer, but it allows the programmer to access information through the keyword interface, so that there will be less expressions to judge. The Web server output in this format is another type of HTML string, called response, which can be operated through streaming data.

Worse, streaming data is very troublesome, which leads to the generation of server pages, such as PHP, ASP, and JSP.

The server PAGE method is suitable for handling simple responses. For example, "show the details of a song", but your decision-making depends on the input, it will be messy. For example, "the display format of pop and rock is different ".

Server Page is good at processing user interaction, while Server Page is good at processing formatted response information. Therefore, it is natural that scripts are used to process request interaction and server pages are used to process response formatting. This is actually the processing of View/controller in the famous MVC (Model View Controller) mode.

Web Server MVC Workflow

The first point of applying the Model View Controller mode is that the model must be completely isolated from the web service. Use the transaction script or domain model mode to encapsulate the processing process.

Next, we will classify the remaining modes into two types: the controller mode and the view mode.

View Mode

There are three view modes: Transform view, template view, and two step view. The processing of transform view and template view is only one step. domain data is converted to HTML. Two Step view requires two steps. The first step is to convert domain data into a logical representation, and the second step is to convert the logical representation to HTML.

The advantage of two-step processing is that the logic can be centralized in one place. If there is only one step, you need to modify each screen when the change occurs. But you need to have a good logical screen structure. If a web application has many front-end users, the two-step processing is particularly useful. For example, the airline ticket booking system. Different logic screens can be obtained through step 2 processing.

The single-step method has two optional modes: Template view and transform view. In the template viewCodeEmbedded in HTML pages, just like the current server page technology, such as ASP, PHP, JSP. This mode is flexible, powerful, but disorganized. If you can organize the logic program logic well outside the page structure, this mode still has its advantages.

Transform view uses the translation method. For example, XSLT. If your domain data is processed in XML, this mode is particularly useful.

Controller mode

Controller has two modes. Generally, we decide a control based on the action. An action may be a button or link. This mode is the action controller mode.

The Front Controller further separates the processing and processing logic of HTTP requests. Generally, only one web handle is used to process all requests. All your HTTP requests are handled by one object. You can minimize the impact of changing the action structure.

Related Article

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.