[Architecture design] 3-layer infrastructure

Source: Internet
Author: User
[Architecture design] 3-layer infrastructure three-layer architecture

As long as you are a software practitioner, whether or not you are from a bachelor's degree, I believe there will be no stranger to the three-tier architecture. In a three-tier architecture, program code generated by software development is classified into the system presentation layer, domain logic layer, and data access layer according to different purposes. Where:

  • The system presentation layer (presentation layer) is used to classify the program code related to "providing operation interfaces. For example, if textbox is provided to receive user input address data, notify the user of processing results through MessageBox, or even provide Web APIs for remote system use, the program code will be classified in the presentation layer.

  • Domain Layer is used to classify program code related to system logic. For example, shopping mall system product data, shopping cart, statement, or attendance system commuting records, employee data... And so on, these program codes will be categorized into the domain logic layer.

  • An access layer is used to classify program code related to "real-time data access. For example, store data to SQL Server, or retrieve data from remote WebService... And so on. These program codes are all stored in the access layer.

By designing a three-tier architecture, developers can roughly split the system into three different categories. By applying such an architecture design, developers can reduce the design content they need to think about, so that they only need to think about a specific design content at a time. In the subsequent maintenance process, developers can also understand the existing program content in different categories.

Infrastructure Design

Although software practitioners are familiar with the three-tier architecture. However, the three-layer architecture was proposed in the early years, along with the invention of object-oriented language, the addition of DI and IOC concepts, and the emergence of technologies such as DDD and TDD. As developers are writing a software system that uses a three-tier architecture, more and more aspects need to be considered. A layer-by-layer knowledge system has slowly dried up the brain cell energy of developers. But fortunately, there is a shortage of software architects in the modern software industry. In the process of project development, the efforts of software architects are used to digest the accumulation of knowledge systems and generate a software architecture that meets the project requirements and team capabilities. In the future, developers in the team can produce system software with a certain level by drawing according to the software architecture.

Next, we will introduce an infrastructure software architecture developed with a three-tier architecture as the core. Developers who do not have software architects are expected to have a basic software architecture for graph construction. In addition to increasing the speed of software development, the program code produced by the project can also meet the following requirements: perform unit tests, reuse system logic, and switch data sources .... And other non-functional requirements.

Domain Layer)

Next, let's look at the domain logic layer in the three-tier architecture to see how to design the objects contained in the domain logic layer. The domain logic layer is mainly used to classify program code related to "encapsulate system logic". The concept of "system logic" can be divided into several modes for classification design:

  • Entity: entity is mainly used to encapsulate data units into objects. When designing a system, the data units to be processed by one system can be encapsulated into Entity objects. The attributes of each entity object are used to provide the data content contained in the data units, such as the employee object and product object used to represent the employee data and product data to be processed by the system. In addition, the entity object method is also used to encapsulate various computing functions. For example, a function to determine whether an employee is an adult can be encapsulated into an employee object for use.

  • Irepository: irepository is mainly used to define the interface (system boundary) of the entity object in and out of the logical layer of the domain ). When designing a system, when an entity object needs to access the database or remote services, it can encapsulate the access function as a method of the irepository interface, for example, the iemployeerepository interface is used to provide the method definition for the entry and exit of the employee object into the system database. In addition, the iemployeerepository interface is also used to encapsulate various query functions. For example, if you want to query all products with prices less than 100 RMB, you can choose to encapsulate them into the iemployeerepository interface.

  • Service: A service is mainly used to encapsulate system functions that are hard to classify. When designing a system, it is not appropriate to assign a system function to an entity object or to the irepository interface, you can consider encapsulating this system function as a service object. For example, if a transfer function involves several accounts and requires many transaction records, assigning them to entity objects and the irepository interface will make the coupling between the two too high, in this case, you can encapsulate an independent transfer object for use.

  • Context: context is mainly used to provide unified entry points for system functions. When designing the system, the system functions will be assigned to entity objects, irepository interfaces, and service objects for use. Then, the added context object will combine the original responsibilities, it can simplify the complexity of use. Another advanced topic is the integration of the context object to provide the irepository interface for injection and storage. It is also a solution to reduce the dependency between the domain logic layer and di framework.

The system logic to be encapsulated in the domain logic layer can be basically built into one object. To apply this design architecture, unit tests can be carried out because the irepository interface is used to construct the system boundary. The context object is used to simplify the system complexity, it makes it feasible to reuse system logic and switch data sources.

System presentation layer)

After talking about the core domain logic layer, it is easier to look at the system presentation layer. The system presentation layer (presentation layer) is used to classify the program code related to "providing operation interfaces". The operation interfaces defined here have an important concept:

  • Ui interface used by the application: the UI interface used by the application. Through the visualized interface provided by the application, the system can provide information to the user and receive the data and operations entered by the user.

  • The API used by the system: the API used by the system is the communication interface of the service program. The Service Program provides various communication interfaces to enable functions for external systems. These interfaces may be WCF, Web Service, or even signalr, it is one of the APIS provided by the system.

Looking back at the design of the entire system presentation layer, simply put, it is to use the functions provided by the domain logic layer to provide interfaces to others and follow interface operations. For example, the domain logic layer provides a "query all products sold for less than 100 RMB" function. When the system provides a UI interface for users, A display page may be provided to list all items queried from the data access layer. When the system provides an API interface for the system to use, A web API method may be created to provide query, and all items queried from the data access layer are uploaded back and forth in JSON format.

Of course, we should not do anything that is not beneficial. The system presentation layer is cut out in the system. A major benefit of the project development process is that the system presentation layer is the most modifiable Design for users. During project creation, the most comments to users are the system presentation layer. When developers split the system presentation layer independently, the logic behind how the screen changes remains unchanged. In this way, the developer can easily face the challenges of the customer (it is only picture adjustment), but in fact it will not change to the logic layer of the core domain, it also avoids the problem of changing this side.

Data access layer)

Finally, let's look at the simplest data access layer in the three-tier architecture. The access layer is used to classify program code related to "Real-time Data Access". The actual data access defined here is as follows, is the irepository interface defined in the logic layer of the implementation domain. However, although it is simple to implement the irepository interface, it also contains many changes:

  • Store data to a database (remote): For general web sites and applications, data is stored on a remote SQL Server.

  • Data is stored in the database (near-end): In some app development projects, the data is stored in the local SQLite.

  • Store data to the system (remote): In some app development projects, data is stored on a remote server through web service.

  • Data is stored in the system (near-end): In some complex development projects, the data source is selected as another domain logic layer. For example, the customer data source of a shopping mall may be provided by another customer management system.

  • Depends on the running status: In some app development projects, the data storage location is determined based on the online network status. For example, SQLite for accessing the local end when the network is disconnected, and WebService for accessing the remote connection when the network is online.

  • ......

Among these solutions listed above, it is interesting that the data is stored in the system and determined by the running status. Data is stored in the system. This change style adds the possibility of concatenating various systems to the system, allowing the system to combine one string and one another like accumulation of wood, greatly increasing the reusability of the system. Depending on the running status, it is the design of a reusable program code, as long as an additional irepository implementation is added to determine the running status, and allocate the existing SQL implementation and web implementation to call, you can reuse existing programs.

Similarly, we should not do anything that is not beneficial. The data access layer is cut out in the system. A major benefit of the project development process is that the data access layer is the most modifiable design in the development process. During project development, many data fields will be discovered by customers in the middle and late stages of the project (amazing, but real ). When developers split the data access layer independently and provide data through object operations before the data field is finalized, the SQL data table is created at the end. In this way, during the development process, you do not need to adjust the data table fields and check the corresponding SQL commands repeatedly, greatly reducing the time cost that developers spend on these trivial tasks.

Download

Sample program code: Click here to download

[Architecture design] 3-layer infrastructure

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.