1. What can OSS provide for us? Before analyzing, let's review the object-oriented features. Objects are everything to be studied. Class is the abstraction of objects of the same or similar nature. Object-oriented elements: encapsulation, inheritance, and polymorphism. Object-oriented objective: how to assign responsibilities. Object-Oriented Design Principle: single responsibility principle (SRP)
1. What can OSS provide for us? Before analyzing, let's review the object-oriented features. Objects are everything to be studied. Class is the abstraction of objects of the same or similar nature. Object-oriented elements: encapsulation, inheritance, and polymorphism. Object-oriented objective: how to assign responsibilities. Object-Oriented Design Principle: single responsibility principle (SRP)
1. What can OSS provide for us?
Before analyzing, let's review the object-oriented features.
ObjectIs anything to be studied.
ClassIs the abstraction of objects of the same or similar nature.
Object-oriented elements: encapsulation, inheritance, and polymorphism.
Object-oriented objective: how to assign responsibilities.
Object-Oriented Design Principles:
- Single Responsibility Principle(SRP) a class has only one reason for its change.
- Open-closed Principle(OCP) (external) scalable, (internal) cannot be modified.
- Lee's replacement principle(LSP) the child type must completely replace its parent type.
- Dependency inversion principle(DIP) depends on abstraction, not on specifics.
- Interface isolation principle(ISP) it is better to use multiple dedicated interfaces than to use a single total interface;
- Principles of merging/aggregation multiplexing(Composite/Aggregate Reuse Principle, CARP) use existing objects in a new object to make it part of the new object; new objects reuse existing functions by assigning to these pairs.
- Minimum knowledge Principle(Principle of Least Knowledge, PLK, also called the dimit Law) do not talk to strangers.
2. Business logic layer
In web applications, we usually useMVCA layered architecture is used to separate duties.
The purpose of the MVC pattern is to implement a dynamic program design, simplify subsequent modifications and extensions of the program, and make reuse of a part of the program possible. In addition, this mode simplifies the complexity to make the program structure more intuitive. The software system also gives the functions of each basic part through the separation of its basic parts.
- Controller(Controller)-responsible for forwarding requests and processing requests.
- View(View)-graphic interface designer.
- Model(Model)-it is used to encapsulate data (attributes) related to the application's business logic and the data processing method (behavior ).
According to this definition, the model is the implementation class of the legendary domain model to implement business logic, but the data access layer does not belong to the model.
The model in MVC can be defined as domain logic.
Domain LogicThere are many medium architecture modes, such:
Transaction script:Use a process to design the business logic. Each process processes a single request from the presentation layer.
Domain Model:A domain object model that combines behavior and data (attributes.
Table module:An instance that processes the business logic of all rows in a database table or view.
Service Layer:Define application boundaries through a service, create a set of available operations at the service layer, and coordinate application responses within each operation.
The table module model combines the domain model and data access.
Transaction scripts are process-oriented, which is not conducive to assigning responsibilities.
The service layer is on the domain model layer, and the system is much more complicated when the service layer is introduced.
If the project is relatively small, the business logic is very simple, and a fixed database (such as MySQL) is used, there is no need to divide the business logic layer and the data access layer, you can select the table module mode to organize the domain logic.
If the project's functions will increase over time and the demand is difficult to control, we need to consider reducing dependencies and clarifying responsibilities, in this way, we use the domain logic architecture pattern of the domain model as the M layer in MVC, describe the attributes and behaviors of the object, and add a data access layer to provide data for the business logic.
3. Data access layer
In martin fowler's enterprise application architecture model, four data source architecture modes are introduced: Table data entry, row data entry, activity record, and data er. In addition, there are also ResultSet and Metadata modes.
(1)Table Data Entry(Table Data Gateway): the object that acts as the database Table access entry. One instance processes all rows in the table.
(2)Row Data Entry(Row Data Gateway): acts as an object for a single record entry in the Data source. One instance per line.
(3)Activity records(Active Record): an object that encapsulates a row in a database table or view, encapsulates database access, and adds domain logic to the data.
(4)Data er(Data Mapper): A ER layer that moves Data between objects and databases while they are independent from each other. It can transmit data in memory objects and databases and keep them independent from each other to separate fields from data sources.
The activity record set can be used at both the business logic layer and the data access layer.
The ideal scenario is to use the data er mode, using the ORM layer, the business logic layer only contributes to the behavior in the field, adding the VO model to describe the object attributes and validate, access the database through DAO. In this way, you can thoroughly target the VO model class in business logic, data access, and view. But back to reality, we found that doing so complicate things.
Our PHP programmers treat SQL statements like C/C ++ programmers without pointers. SQL statements are flexible and more conducive to performance optimization. In most cases, the database used by our system is pre-determined and there is little possibility of transfer. In addition, when we use object-oriented analysis, we only analyze the business logic layer (domain model ). We only use kv arrays/objects to replace VO objects. in PHP, both data storage and data analysis and processing are more flexible. Therefore, the language determines that the table data import/export mode is more suitable for the PHP Data access layer.
In summary, the model in MVC isDomain object modelIs responsible for the attributes and actions of the business object, which forms the business logic layer.Table Data EntryMode to support the business logic layer.
Original article address: PHP business logic layer and data access layer design, thanks to the original author for sharing.