Data source schema mode row data entry

Source: Internet
Author: User
Note: Do not understand do not tread, this article is not for Java,java enthusiasts can skip directly.

First, the concept

Row Data Gateway: the object that acts as a single record entry in the data source, one instance per line.

Second, the simple implementation of the row data entry

To facilitate understanding, it is simple to implement:

<?php/** * Row data entry class */class Ordergateway {/* Define metadata Map */private $_name;     Private $_id;        Public function __construct ($id, $name) {$this->setid ($id);    $this->setname ($name);    } public Function GetName () {return $this->_name;    The Public Function SetName ($name) {$this->_name = $name;    } public Function GetId () {return $this->_id;    The Public Function setId ($id) {$this->_id = $id; }/** * The Ingress class itself has an update operation */Public Function update () {$data = array (' id ' = = $this->_id, ' name ' =&G T         $this->_name);        $sql = "UPDATE order SET"; foreach ($data as $field = = $value) {$sql. = "'". $field. "` = '" . $value.        "',";        } $sql = substr ($sql, 0,-1); $sql. = "WHERE id =".        $this->_id;    Return Db::query ($sql); }/** * The entry class itself has an insert operation */Public Function Insert () {$data = array (' name ' = = $this->_nAME);        $sql = "INSERT into order"; $sql. = "('". Implode ("', '", Array_keys ($data)).        "`)"; $sql. = "VALUES ('". Implode ("', '", Array_values ($data)).         "')";    Return Db::query ($sql);  The public static function load ($RS) {/* Here can be added cache */return new Ordergateway ($rs [' id ']? $rs [' ID ']: NULL,    $rs [' name ']); }}/** * In order to read information from the database, set up the standalone Orderfinder bellow. */class Orderfinder {public function find ($id) {$sql = ' select * FROM order WHERE id = '.        $id;         $rs = Db::query ($sql);        Return Ordergateway::load ($RS);//The Row object returned here} public Function FindAll () {$sql = ' select * from order ';         $rs = Db::query ($sql);        $result = Array ();            if (Is_array ($rs)) {foreach ($rs as $row) {$result [] = Ordergateway::load ($row);    }} return $result; }} class DB {/** * This is just a demonstration method of executing SQL * @param string $sql The SQL */public static function query that needs to be executed ($sql) {echo "Execute sql:", $sql, "<br/>"; }}/** * The client calls */class clients {public static function main () {header ("content-type:text/html;         Charset=utf-8 ");        /* Write Example */$data = array (' name ' = = ' start ');        $order = Ordergateway::load ($data);         $order->insert ();        /* Update Example */$data = array (' id ' = = 1, ' name ' = ' Stop ');        $order = Ordergateway::load ($data);        $order->setname (' xxxxxx ');         $order->update ();        /* Query Example */$finder = new Orderfinder ();        $order = $finder->find (1);    echo $order->getname (); }} client::main ();? >

Third, the operating mechanism

A row data entry is an object that is extremely similar to a single record, in which each column in the database is a domain.

Row data portals can typically implement arbitrary conversions from a data source type to an in-memory type.

The row data entry does not have any realm logic and, if present, is the activity record.

As you can see in the instance, to read information from the database, set up a separate Orderfinder class. Of course, you can also choose not to create a new class, using a static lookup method, but it does not support polymorphism that requires different lookup methods for different data sources. Therefore it is best to set the object of the Find method separately.

Row data portals can also be used for views in addition to tables. It is important to note that the update operation for the view.

It is good practice to have "define metadata mappings" visible in your code, so that all database access code can be automatically generated during the auto-build process.

Iv. use of the scene

4.1 Transaction Scripts

Database access code can be well separated and easily reused by different transactional scripts. However, you may find that the business logic recurs in multiple scripts that might be useful in the data entry. Moving these logic constantly makes the row data entry into an activity record, which reduces the duplication of business logic.

4.2 Domain Models

If you want to change the structure of the database but do not want to change the domain logic, using row data entry is a good choice. In most cases, the data mapper is more appropriate for the domain model.

The row data entry can be used in conjunction with the data mapper, although this may seem a bit superfluous, but it works well when the row data entry is automatically generated from the metadata and the data mapper is implemented manually.

4.3 Table module (not considered)

  • 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.