Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the ActiveRecord pattern differ from the Domain Object or Data Mapper pattern?

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?

like image 966
Joe Van Dyk Avatar asked Sep 18 '08 15:09

Joe Van Dyk


People also ask

What's the purpose of Active Record?

Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.

Is Active Record an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.

Is Active Record an anti pattern?

In software engineering, the active record pattern is considered an architectural pattern by some people and as an anti-pattern by some others recently. It is found in software that stores in-memory object data in relational databases.

Is an ORM a data mapper?

ORM is a special data mapping layer, which provides database access between the app and database, i.e. it facilitates the communication of an application with a database.


2 Answers

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.

like image 151
thr Avatar answered Nov 09 '22 07:11

thr


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.

like image 37
user17384 Avatar answered Nov 09 '22 09:11

user17384