views:

799

answers:

2

I was looking at DataMapper, which appeared at first glance to use the ActiveRecord ORM pattern. Other people said that it uses the DataMapper and/or the Domain Object pattern.

What is the difference between those patterns?

+10  A: 

The main difference between the two patterns is this:

  • In the ActiveRecord you have one domain object that both knows all the business logic and how to save/update itself in the database, user.getLinkToProfile() and User::find(1), User::save(user)

  • In the DataMapper pattern you have one domain object that holds all the business logic, for exmaple user.getLinkToProfile() (or something similar) but knows nothing about the database in question, in addition to this you have a mapper-object that is responsible for saving, updating, selecting, etc. user objects from the database which would have UserMapper::find(1), UserMapper.save(user)

DataMapper is potentially more complex then ActiveRecord but it's a lot easier to develop your domain model and database asynchronous then with ActiveRecord.

thr
+2  A: 

Active record is very heavy, data mapper and domain object are separating those concerns out so you have a more defined set of code doing various aspects for you "domain" or "entity" objects.

I personally prefer, not that you asked, going with the separation into domain object, data mapper, probably use an assembly pattern and even a data transfer pattern to assure clear separation of what happens to data between the database an the upper tiers of an application.

...elegant and simple separations always help.