Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Active Record and Table Data Gateway

What is the main or what are the main differences between the active record pattern and the table data gateway pattern?

I am relatively new to programming so apologies if it is a dumb question but they seem so similar to me.

like image 650
David Avatar asked Sep 17 '11 20:09

David


2 Answers

This answer is like most patterns answers really just my interpretation, most of what I'm saying is based upon the Active Record and Table Data Gateway descriptions on Martin Fowler's site.

Key quotes are this about Table Data Gateway:

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

And this about Active Record:

An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.

So at the simplest level, Table Data Gateway abstracts a database table while Active Record wraps a row from a table.

The other key point I take from reading that is that Active Record objects are Domain Objects, meaning that the object that you call Update() on to save your database changes is also the object that contains business logic. With the Table Data Gateway this is not the case, the gateway will usually act as a layer which translates the output from your database calls into Domain objects and when you want to persist changes to those objects you pass the back to the gateway and tell the gateway to save.

Finally, that fact the Table Data Gateway works on a whole table is important in terms of design and how you think about your data access.

I've never particuarly used Active Record but have worked on a big project that used Table Data Gateway in its design. It is a solid pattern but many would say it is quite dated - the advantages you get of keeping all your data access in one places and allowing your DBAs to own the database have been over taken by the new advantages of ORMs which give developers good object oriented methods of working with data with the corresponding advantages in terms of development time and transparancy.

(I'm not really keen on commenting either way on how much of the above paragraph is true - I'm just trying to represent some of the community opinion. I currently use an ORM but could also happily go back to the gateway approach)

like image 127
David Hall Avatar answered Sep 22 '22 05:09

David Hall


This is just example for implementation both
Active Record

  class ActiveRecord
  {
    protected $connection = null;

    public function __construct()
    {
      $this->connection = new PDO("mysql:host=localhost; dbname=db_userscloud", 'root', '');
    }

    public function loadId($id)
    {
      $sql    = 'SELECT * FROM blog WHERE id_blog = ' . (int) $id;
      $result = $this->connection->query($sql);

      $record = $result->fetch(PDO::FETCH_ASSOC);

      foreach($record as $column => $value) {
        $this->$column = $value;
      }

    }
  }

  $blog  = new ActiveRecord();
  $blog->loadId(1);

  echo $blog->id_blog . '<br />'; 
  echo $blog->title_blog;


Table Data

  class TableData
  {
    protected $connection = null;

    public function __construct()
    {
      $this->connection = new PDO("mysql:host=localhost; dbname=db_userscloud", 'root', '');
    }

    public function loadId($id)
    {
      $sql    = 'SELECT * FROM blog WHERE id_blog = ' . (int) $id;
      $result = $this->connection->query($sql);

      return $result->fetch(PDO::FETCH_ASSOC);                          
    }   

  $gateway  = new TableData();
  $blog     = $gateway->loadId(1);

  echo $blog['id_blog'] . '<br />'; 
  echo $blog['title_blog'];
like image 27
antelove Avatar answered Sep 26 '22 05:09

antelove