PHP business Logic Layer and data access layer design

Source: Internet
Author: User

The following still feel a little bit abstract

1, object-oriented can give us what?

Before we do the analysis, let's review the object-oriented.
An object is anything that is to be studied.
A class is an abstraction of an object that has the same or similar properties.
Object-oriented features: encapsulation, inheritance, polymorphism.
Object-oriented purposes are: How to assign Responsibilities .

Object-Oriented design principles :

    • single principle of responsibility (SRP) A class that has only one cause for it to change.
    • Open-closed principle (OCP) (externally) extensible, non-modifiable (internal).
    • Lee's replacement principle (LSP) subtype must be able to completely replace its parent type.
    • Dependency Inversion principle (DIP) depends on abstraction, not on specifics.
    • Interface Isolation principle (ISP) Using multiple specialized interfaces is better than using a single total interface;
    • The synthesis/aggregation multiplexing principle (composite/aggregate reuse Principle,carp) uses some existing objects inside a new object to make it part of the new object The new object is used to reuse the existing functionality by delegating to these pairs.
    • Minimum Knowledge principle (Principle of Least Knowledge,plk, also known as Dimitri Law) don't talk to strangers.

2. Business Logic Layer
In Web applications, we typically use the MVC layered Architecture pattern to separate responsibilities.
The purpose of the MVC pattern is to implement a dynamic program design that simplifies the subsequent modification and expansion of the program, and makes it possible to reuse a part of the program. In addition, this mode makes the program structure more intuitive by simplifying the complexity. By separating the basic parts of the software system, it also gives the functions of each basic part.

    • Controller -Responsible for forwarding the request and processing the request.
    • View – Graphical interface design for interface designers.
    • model – Used to encapsulate data (attributes) related to the business logic of the application and how to handle the data (behavior).

According to this definition, the model is the realization class of the legendary domain model, realizes the business logic, but the data access layer is not the model.
The models in our MVC can be defined as domain logic.
domain Logic has multiple schema patterns, such as:
Transactional scripting: use procedures to design business logic, each of which processes individual requests from the presentation layer.
domain model: A Domain object model that incorporates behavior and data (attributes).
Table module: An instance of the business logic that processes all rows in a database table or view.
Service Layer: defines application boundaries through a service, establishes a set of available operations in the service layer, and coordinates the response of the application within each operation.

The table module model combines domain models and data access.
Transactional scripting is process-oriented and is not conducive to assigning responsibilities.
The service layer is a layer in the domain model, the system is much more complicated when the service layer is introduced.

If the project is relatively small, the business logic is simple, but also with a fixed database (such as MySQL), you do not need to split the business logic layer and the data access layer, you can choose the Table module schema organization domain logic.
If the functionality of the project becomes more and more difficult to control, we need to take more account of the reduced dependencies and clear responsibilities, so that we use the domain logical schema pattern of the domain model as the M layer in MVC, describe the properties and behavior of the object, and add a data access layer to the business logic to provide the data.

3. Data Access Layer
The "Enterprise Application Architecture Model" in Martin Fowler introduces 4 data source schema modes: Table data entry, row data entry, activity record, data mapper. In addition, there are resultset, metadata and other modes.
(1) table Data Gateway: an object that acts as a database table access portal. One instance handles all rows in the table.
(2) row data Gateway: an object that acts as a single record entry in the data source. One instance per line.
(3) activity record : An object that wraps a row in a database table or view, encapsulates database access, and adds domain logic to the data.
(4) data Mapper: A mapper layer that moves data between objects and databases when they are kept separate from each other. It can pass data in memory objects and databases and keep them separate from each other to separate domains from the data source.

The active Recordset can be used in situations where the business logic layer and the data access layer are not divided.

Ideally, using the data mapper pattern, using the ORM layer, the business logic layer only forces the behavior in the field, increasing the VO model to describe and validate the properties of the object, and access the database through DAO. This can be completely object-oriented, in business logic, data access, view directly manipulate the intuitive Vo model class. But back in the real world, we find that doing this complicates things.
We PHP programmers on SQL statements like a C + + programmer without pointers, the use of SQL statements is very flexible, more conducive to performance optimization. In most cases, the database used by our system is pre-determined and the likelihood of transfer is very low. And when we use object-oriented analysis, we only analyze the business logic layer (domain model). We use only the KV array/object instead of the Vo object in PHP, whether it is data storage or data analysis and processing, are more flexible. Therefore, by the language itself, the table data entry mode is more suitable for PHP data access layer.

In conclusion, our model in MVC is the implementation of the domain object model , which is responsible for the property and behavior of the business object, and forms the business logic layer, which supports the business logic layer through the table data entry mode.

PHP business Logic Layer and data access layer design

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.