Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the significant difference between active record and data mapper based ORMs?

Like doctrine(active record) and Xyster(data mapper),what's the difference?

like image 428
user198729 Avatar asked Mar 10 '10 03:03

user198729


People also ask

What is Active Record Data Mapper?

What is the Active Record pattern? In TypeORM you can use both the Active Record and the Data Mapper patterns. Using the Active Record approach, you define all your query methods inside the model itself, and you save, remove, and load objects using model methods.

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.

What is data mapper in ORM?

From P of EAA: Data Mapper is a layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself. ORM (Object Relational Mapping) is an possible implementation of Data Mapper.

When would you use a data mapper?

This is useful when one needs to model and enforce strict business processes on the data in the domain layer that do not map neatly to the persistent data store. The layer is composed of one or more mappers (or Data Access Objects), performing the data transfer.


2 Answers

The difference is in how separate your domain objects are from the data access layer. With ActiveRecord, its all one object, which makes it very simple. Especially if your classes map one to one to your database. Data mapper is more flexible, and easily allows your domain to be tested independent of any data access infrastructure code. But complexity comes at a price.

like image 108
blockhead Avatar answered Jan 18 '23 23:01

blockhead


Like blockhead said, the difference lies in how you choose to separate Domain Objects from the Data Access Layer.

In a nutshell, Active"Record" maps an object to a record in the database.

Here, One Object = One Record.

From what I know, Data"mapper" maps an object with data, but it need not be a record - it could be a file as well.

Here, One Object need not be One Record

It's this way because the goal of this pattern: to keep the in memory representation and the persistent data store independent of each other and the data mapper itself.

By not placing this 1 object = 1 record restriction, Data Mapper makes these two layers independent of each other.

Any suggestions/corrections to my answer are welcome, in case I was wrong somewhere.

like image 35
Bharadwaj Srigiriraju Avatar answered Jan 18 '23 22:01

Bharadwaj Srigiriraju