[Intention of activity record]
An object that encapsulates a row in a data table or view, encapsulates database access, and adds domain logic to the data.
[Applicable scenarios of activity records]
Applicable to less complex domain logic, such as CRUD operations.
[Operation Mechanism of activity records]
Objects have both data and behavior. It uses the most direct method to place the data access logic in the domain object.
Activity records are essentially a domain model. The classes in this domain model should be exactly matched with the record structures in the database. each field of the class corresponds to each column of the table.
Generally, activity records include the following methods:
1. Construct an activity record instance from a data row;
2. construct a new instance for future table inserts;
3. Use static search methods to encapsulate common SQL queries and return activity records;
4. update the database and insert the data in the activity record into the database;
5. Obtain or set the domain;
6. implement some business logic.
[Advantages and disadvantages of activity records]
Advantages:
1. simple, easy to create, and easy to understand.
2. Reduce code replication when using transaction scripts.
3. Domain logic is not changed when the database structure is changed.
4. derivation and test verification based on a single activity record are very effective.
Disadvantages:
1. No hidden relational database exists.
2. The activity record is valid only when the activity record object is directly matched with the table in the database.
3. requires tight coupling between object design and database design, which makes it difficult to further reconstruct the project.
Activity record and other modes]
Data source architecture mode: the row data entry is similar to the row data entry. The main difference between the two is that the row data entry only has database access, and the activity records have both data source logic and domain logic.
PHP example of activity record]
- <? Php
- /**
- * Activity record of enterprise application architecture data source architecture mode 2010-10-17 sz
- * @ Author phppan. p # gmail.com http://www.phppan.com
- * Members (http://www.blog-brother.com /)
- * @ Package architecture
- */
- /**
- * Order Type
- */
- Class Order {
- /**
- * Order ID
- * @ Var <type>
- */
- Private $ _ order_id;
- /**
- * Customer ID
- * @ Var <type>
- */
- Private $ _ customer_id;
- /**
- * Order amount
- * @ Var <type>
- */
- Private $ _ amount;
- Public function _ construct ($ order_id, $ customer_id, $ amount ){
- $ This-> _ order_id = $ order_id;
- $ This-> _ customer_id = $ customer_id;
- $ This-> _ amount = $ amount;
- }
- /**
- * Instance deletion
- */
- Public function delete (){
- $ SQL = "DELETE FROM Order SET WHERE order_id =". $ this-> _ order_id. "AND customer_id =". $ this-> _ customer_id;
- Return DB: query ($ SQL );
- }
- /**
- * Instance update operations
- */
- Public function update (){
- }
- /**
- * Insert operation
- */
- Public