Data Source Architecture mode-data er

Source: Internet
Author: User
The preceding section describes the data source architecture mode, including table data entry, data source architecture mode, and data entry data source architecture mode. compared with the three data source architecture modes, data ing... the preceding section describes the data source architecture mode, including table data entry, data source architecture mode, and data entry data source architecture mode. compared with the three data source architecture modes, the data er seems to be "too tall ".

I. concepts

Data ER: a er layer that moves Data between objects and databases (and the er itself) when they are independent of each other. The concept is always abstract. Simply put, a data mapper is a class data that maps data to objects.

2. Why do we need to use a data er?

The implementation of the data er is more complex than the first three modes. why should we use it?

The organizational relationships between objects are different from those in the relational database. A database table can be viewed as a grid composed of rows and columns. a row in a table can be associated with a row in another table (or even the same table) through a foreign key. the organizational relationship of objects is more complex: an object may contain other objects. different data structures may organize the same objects in different ways.

The difference between objects and relational databases is called "object link impedance mismatch" or "impedance mismatch ".

The data er can solve this problem well and is responsible for data conversion between objects and relational databases, this effectively hides database operations in the domain model and manages conflicts that cannot be avoided during database conversion.

III. simple data er

Php code

 MarkNew () ;}else {$ this-> id = $ id ;}} function getId () {return $ this-> id;} static function getCollection ($ type) {// Here we generate the returned HelperFactory: getCollection ($ type);} function collection () {return self :: getCollection (get_class ($ this);} function finder () {return self: getFinder (get_class ($ this);} static function getFinder ($ type) {// Here, the map object r corresponding to this object is generated through a factory Eturn HelperFactory: getFinder ($ type);} function setId ($ id) {$ this-> id = $ id;} function _ clone () {$ this-> id =-1 ;}// class Venue extends DomainObject {private $ name; private $ spaces; function _ construct ($ id = null, $ name = null) {$ this-> name = $ name; parent ::__ construct ($ id);} function setSpaces (SpaceCollection $ spaces) {$ this-> spaces = $ spaces;} function getSpaces () {if (! Isset ($ this-> spaces) {// create the corresponding SpaceMapper object $ finder = self: getFinder ('space '); $ this-> spaces = $ finder-> findByVenue ($ this-> getId (); // $ this-> spaces = self: getCollection ("Space ");} return $ this-> spaces;} function addSpace (Space $ space) {$ this-> getSpaces ()-> add ($ space ); $ space-> setVenue ($ this);} function setName ($ name_s) {$ this-> name = $ name_s;} function getName () {return $ This-> name;} static function findAll () {$ finder = self: getFinder (_ CLASS _); return $ finder-> findAll ();} static function find ($ id) {$ finder = self: getFinder (_ CLASS _); return $ finder-> find ($ id );}} abstract class Mapper {protected static $ PDO; function _ construct () {if (! Isset (self ::$ PDO) {// cache self ::$ PDO = new PDO ($ dsn); self ::$ PDO-> setAttribute (PDO :: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION) ;}} private function getFromMap ($ id) {// retrieve this $ id DomainObject from memory} private function addToMap (DomainObject $ obj) {// add this DomainObject to the memory} function find ($ id) {$ old = $ this-> getFromMap ($ id); if ($ old) {return $ old;} $ this-> selectstmt ()-> execute (array ($ id ); $ Array = $ this-> selectstmt ()-> fetch (); $ this-> selectstmt ()-> closeCursor (); if (! Is_array ($ array) {return null;} if (! Isset ($ array ['id']) {return null;} $ object = $ this-> createObject ($ array); return $ object;} function findAll () {$ this-> selectAllStmt ()-> execute (array (); return $ this-> getCollection ($ this-> selectAllStmt ()-> fetchAll (PDO :: FETCH_ASSOC);} function createObject ($ array) {$ old = $ this-> getFromMap ($ array ['id']); if ($ old) {return $ old;} $ obj = $ this-> doCreateObject ($ array); $ th Is-> addToMap ($ obj); return $ obj;} function insert (DomainObject $ obj) {$ this-> doInsert ($ obj ); $ this-> addToMap ($ obj);} protected abstract function getCollection (array $ raw); protected abstract function doCreateObject (array $ array ); protected abstract function doInsert (DomainObject $ object); protected abstract function targetClass (); protected abstract function selectStmt (); protect Ed abstract function selectAllStmt ();} class VenueMapper extends Mapper {function _ construct () {parent ::__ construct (); $ this-> selectAllStmt = self :: $ PDO-> prepare ("SELECT * FROM venue"); $ this-> selectStmt = self: $ PDO-> prepare ("SELECT * FROM venue WHERE id =? "); $ This-> updateStmt = self: $ PDO-> prepare (" UPDATE venue SET name = ?, Id =? WHERE id =? "); $ This-> insertStmt = self: $ PDO-> prepare (" INSERT into venue (name) values (?) ");} Function getCollection (array $ raw) {// use an array of objects $ ret = array (); foreach ($ raw as $ value) for simplicity) {$ ret [] = $ this-> createObject ($ value);} return $ ret;} protected function doCreateObject (array $ array) {$ obj = new Venue ($ array ['id']); $ obj-> setname ($ array ['name']); // $ space_mapper = new SpaceMapper (); // $ space_collection = $ space_mapper-> findByVenue ($ array ['id']); // $ obj-> setSpaces ($ Space_collection); return $ obj;} protected function targetClass () {return "Venue";} protected function doInsert (DomainObject $ object) {$ values = array ($ object-> getname (); $ this-> insertStmt-> execute ($ values); $ id = self :: $ PDO-> lastInsertId (); $ object-> setId ($ id);} function update (DomainObject $ object) {$ values = array ($ object-> getname (), $ object-> getid (), $ object-> getId (); $ th Is-> updateStmt-> execute ($ values);} function selectStmt () {return $ this-> selectStmt;} function selectAllStmt () {return $ this-> selectAllStmt ;}} // client code $ venue = new venue (); $ venue-> setName ("XXXXXXX"); // Insert a data entry $ mapper = new VenueMapper (); $ mapper-> insert ($ venue); // get the newly inserted data $ venueInfo = $ mapper-> find ($ venue-> getId ()); // modify data $ venue-> setName ('ooooooooooo'); $ mapper-> update ($ venue);?>

Some helper classes are omitted in the code to retain the most important domain objects and data mappers. The most powerful part of the data ing mode is the elimination of coupling between the domain layer and database operations. Mapper objects work behind the scenes and can be applied to various object relationship mappings. In addition, a large number of specific Mapper classes need to be created. But now the framework can be automatically generated through the program.

IV. time to use

Database mappers are mainly used when Database Solutions and object models need to evolve independently. The most common method is to use it with the domain model. Whether it is in the design, development, or test phase, you can ignore the database when operating on the domain model. Domain objects do not know the database structure, because all these mappings are completed by the data er.

Of course, the data er introduces a new level. Therefore, the prerequisite for using these modes is the complexity of the business logic. if it is very simple, it is unnecessary.

Without a domain model, I will not use a data er. But can I use a domain model when there is no data er? If the domain model is simple and the database is controlled by the domain model developer, it is reasonable for the domain object to directly access the database with activity records.

You do not have to create a database ing layer in the full sense. It is very complicated to create such a data er. In most cases, we recommend that you use the open source database ing layer instead of creating it by yourself.

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.