Friday, December 14, 2018

Table Data Gateway Pattern





An object that acts as a Gateway (466) to a database table. One instance handles all the rows in the table.



Active Record Pattern mixes everything in one class and it is Row Oriented. It puts Data, Getter Setters, Persistence Logic and Domain Logic in one class. this is BBOM. Table Data Gateway is an improvement on the ARP. It segregates the Persisence Logic from the Data and Domain Logic. Figure does say anything about the Domain Logic but it chould be there

Class Person
{
       Data Members:
       Getter Setters
       Domain Logic Methods
}

Class PersonGateway
{

CRUD (Primitive Data Types)

}

Look at the arrow in the figure above, Person Class is associated to the PersonGateway Class

Persistence Methods may be called as follows

PersonGatewayObj.CRUDMethods(person.DataMember);



Table Data Gateway vs. Data Mapper Pattern

==========================================================
the Data Mapper will receive the Domain Model object(Entity) as param and will use it to implement the CRUD operations
Data Mapper takes the Entity and maps it to the underlying Table
Data Mapper is also Table Oriented, provide Table specific CRUD API
IT KNOWS ABOUT THE ENTITY


the Table Data Gateway will receives all the params(as primitives) for the methods and will not know anything about the Domain Model object(Entity).
TDG takes the Primitive Data Types and maps them to the underlying Table
TDG is also Table Oriented, provide Table specific CRUD API
IT DOES NOT KNOW ABOUT THE ENTITY
In the end both of them will act as mediator between the in-memory objects and the database.
Data Mapper (Entity) == Table Data Gateway (Primitive Data Types)



Table Data Gateway

An object that acts as a Gateway to a database table. One instance handles all the rows in the table.
It's a Data Access Object.
!php
$table = new BananaGateway(new Connection('...'));

// Insert a new record
$id = $table->insert('My favorite banana');

// Update it
$table->update($id, 'THE banana');

// Delete it
$table->delete($id);

CRUD

A DAO implements the well-known Create Read Update Delete methods.
Read should not be a single method: Finders to the rescue!

Table Data Gateway

Implementation

!php
class BananaGateway
{
    private $con;

    public function __construct(Connection $con)
    {
        $this->con = $con;
    }

    public function insert($name) {}

    public function update($id, $name) {}

    public function delete($id);
}

Table Data Gateway

The insert method

!php
/**
 * @param string $name The name of the banana you want to create
 *
 * @return int The id of the banana
 */
public function insert($name)
{
    // Prepared statement
    $stmt = $this->con->prepare('INSERT INTO bananas VALUES (:name)');

    $stmt->bindValue(':name', $name);

    $stmt->execute();

    return $this->con->lastInsertId();
}

Table Data Gateway

The update method

!php
/**
 * @param int    $id   The id of the banana to update
 * @param string $name The new name of the banana
 *
 * @return bool Returns `true` on success, `false` otherwise
 */
public function update($id, $name)
{
    $stmt = $this->con->prepare(<<<SQL
UPDATE bananas
SET name = :name
WHERE id = :id
SQL
    );

    $stmt->bindValue(':id', $id);
    $stmt->bindValue(':name', $name);

    return $stmt->execute();
}

Table Data Gateway

The delete method

!php
/**
 * @param int $id The id of the banana to delete
 *
 * @return bool Returns `true` on success, `false` otherwise
 */
public function delete($id)
{
    $stmt = $this->con->prepare('DELETE FROM bananas WHERE id = :id');

    $stmt->bindValue(':id', $id);

    return $stmt->execute();
}

Table Data Gateway

Finders

!php
// Retrieve all bananas
$bananas = $table->findAll();

// Find bananas by name matching 'THE %'
$bananas = $table->findByName('THE %');

// Retrieve a given banana using its id
$banana = $table->find(123);

// Find one banana by name matching 'THE %'
$banana = $table->findOneByName('THE %');
Use the __call() magic method to create magic finders:http://www.php.net/manual/en/language.oop5.overloading.php#object.call.



Table data gateway pattern

An object that acts as a Gateway to a database table. One instance handles all the rows in the table.

How it works

A simple interface that directly maps onto the database. The class contains all the SQL to access the data from the database and simply return a RecordSet (or similar) of the data. The entire system is stateless, and any changes to the underlying database needs to be done with the Gateway class.

When to use it

For very simple database access. Stored procedures themselves are often organized as Table Data Gateways

Example

Table data.png

No comments:

Post a Comment