9

Possible Duplicate:
ORM/DAO/DataMapper/ActiveRecord/TableGateway differences?

Can someone help me to understand difference between data mapper and data access object patterns? I already know about data mapper pattern from zandstra's book. But when i searched for data access object, i found out that there are very similar, or even the same patterns. Also i want to know which one i should use when writing my own framework(i'm trying to implement my own mvc php framework just to learn how modern frameworks work).

Community
  • 1
  • 1
warmspringwinds
  • 1,037
  • 2
  • 13
  • 28
  • Check out below link http://stackoverflow.com/questions/804751/what-is-the-difference-between-the-data-mapper-table-data-gateway-gateway-da –  Sep 04 '12 at 19:08
  • https://stackoverflow.com/questions/491938/whats-the-difference-between-dao-and-data-mapper – Bergi Jun 30 '19 at 21:19

1 Answers1

9

Data mapper saves the data from (and restores to) domain object directly, while data access object would be used as intermediary for exchange of information between domain object and storage abstraction.

<update> The main difference between two approaches is that data mapper temporary takes control of the domain object, while data access object either receives data indirectly (through some higher level abstraction, like Service) or is controlled (and in some implementations, even instantiated) by domain object.</update>

Neither of patterns is remotely related to active record (anti)pattern, which combines domain logic and storage abstraction in single instance, thus breaking SRP.

And none of mentioned patterns are tied to ORMs. Some ORMs try to use the above mentioned pattern for implementation, but they usually do a quite bad job at that.

Most of, what you call, "modern frameworks" use active record pattern and call the instances of it "models", which extreme simplification of concept, perpetuated by Rails.

Marcin Nabiałek
  • 98,126
  • 37
  • 219
  • 261
tereško
  • 56,151
  • 24
  • 92
  • 147
  • This information is complete useless for a php starter! – JvdBerg Sep 04 '12 at 19:28
  • 2
    [PHP 5 Objects, Patterns, and Practice](http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804) is not something that "php starters" *(whatever that means)* would read. – tereško Sep 04 '12 at 19:33
  • 5
    @JvdBerg And? Data mappers, et all are advanced topics. – Mike B Sep 04 '12 at 19:36
  • @tereško Thanks, so as i can assume we can call data access object ' a temporary storage' of data from database before we can write it to domain model. Correct me if i'm wrong, please. – warmspringwinds Sep 04 '12 at 19:42
  • 2
    @JvdBerg: This whole question isn't meant for a PHP starter. – Madara's Ghost Sep 04 '12 at 19:49
  • @user1606150 , the data access object is more like a translator, which lets both domain object and your storage abstraction to exchange the information in a for that they expect. It would be quite common for a DAO class to implement two interfaces, one describing the contract with storage abstraction and other - as contract for domain object. For "temporary storage" you would use some sort of cache, which would be on the other side of storage abstraction. – tereško Sep 04 '12 at 19:50
  • @tereško My interpretation is that `DAO` doesn't requires domain objects. Actually, it's used within `DTO`s in the Core J2EE Patterns: http://www.corej2eepatterns.com/Patterns2ndEd/DataAccessObject.htm Data Mappers, on the other hand, deals with domain objects. – Keyne Viana Sep 05 '12 at 22:19
  • @Keyne , I never said that DAOs ***require*** domain objects. – tereško Sep 05 '12 at 22:39
  • @tereško Then, what do you mean by: "the data access object is more like a translator, which lets both `domain object` and your storage abstraction to exchange the information in a for that they expect" – Keyne Viana Sep 05 '12 at 23:14
  • @Keyne it means that the same DAO is shared between domain object and the storage abstraction. It is used to facilitate exchange of information between them and compensate between differences. For example, if DB save some measurements in metric system but domain logic uses imperial, then DAO will have setters and getters for both. – tereško Sep 06 '12 at 00:03