15

Can you, please, explain me the differences between the following database representatives, say, in PHP.:

ORM DAO DataMapper ActiveRecord TableGateway

Any examples would be appreciated.

Miroslav Asenov
  • 173
  • 1
  • 7
  • related: [What is the difference between the Data Mapper, Table Data Gateway (Gateway), Data Access Object (DAO) and Repository patterns?](https://stackoverflow.com/q/804751/1048572) – Bergi Jun 30 '19 at 21:20

1 Answers1

19

That would require a pretty long answer. Instead of repeating what others have said better and in more detail before me, I link you to some relevant pages. I suggest to look through them. Maybe follow a few additional links. Wikipedia is always a good start. If you still have any questions about one or the other pattern after going through the links, feel free to come back to SO and ask again. But if you do, try to narrow it down. It's better to ask multiple questions and focus on particular aspects than expecting people to write an essay for you.

Object Relational Mapper

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages.

Data Access Object

Use a Data Access Object (DAO) to abstract and encapsulate all access to the data source. The DAO manages the connection with the data source to obtain and store data.

DataMapper

A layer of Mappers (473) that moves data between objects and a database while keeping them independent of each other and the mapper itself.

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.

Table Data Gateway

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

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • I feel like Table Data Gateway and DataMapper are largely the same pattern. They both keep domain objects separate from queries, and both need a way of mapping an object's data to the database. The only difference I see is that Table Data Gateway requires you to explicitly inject each field, whereas DataMapper can accept an object and handles the mapping on its own. Is that correct? – AgmLauncher Oct 25 '13 at 05:21
  • @AgmLauncher No. TableDataGateways handles rows, not Domain Objects. That is, TableDataGateways return a 1:1 representation of the record in the database. It's only when you start to map these to an independently structured Domain object that you get a DataMapper. – Gordon Oct 25 '13 at 05:59
  • I see, so they are not mutually exclusive patterns, the TDG seems to be slightly lower in level than a DM, and a DM can make use of one (or many?) TDG if so desired. Is that correct? – AgmLauncher Oct 25 '13 at 06:00
  • @Gordon According to [Martin Fowler](http://martinfowler.com/eaaCatalog/index.html) **TableDataGateway** handles tables, while **RowDataGateway** handles rows. – IntelliData Jul 05 '16 at 14:06
  • @IntelliData umm, not sure what you are trying to tell me? You do realize that I am linking to the patterns on that very page you linked and that the descriptions are quotes from the respective pattern pages, do you? – Gordon Jul 05 '16 at 14:10
  • @Gordon Please forgive me if I misunderstood what you said. **In your comment**, you said **TableDataGateways handles rows**; according to Fowler, **TableDataGateway** handles the whole table, while **RowDataGateway** handles rows. BTW, when I commented I did not realise we were quoting the same sources. – IntelliData Jul 05 '16 at 17:28
  • @IntelliData the difference between TDG and RDG is how they encapsulate access. With TDG you have one instance providing access to *all the rows in the table*. See the citation above. It's taken directly from Fowler's POEAA catalogue. With RDG you have one instance per row. With TDG you can return many rows. But both handle rows. – Gordon Jul 05 '16 at 18:28
  • @Gordon Ah, you meant **row(s)** as opposed to **Domain Objects**. Thanks for the explanation! :) – IntelliData Jul 05 '16 at 19:31