What's the difference between a Data Access Object and Active Record? They seem to be quite the same, as both built a layer between the application and persistence layer, and abstract away direct database access using SQL queries.
2 Answers
A Data Access Object (DAO) is an interface dedicated to the persistence of a model/domain object to a data-source. Here's a reference.
The ActiveRecord pattern works in a similar fashion, but puts the persistence methods on the model object itself, while the DAO defines a discrete interface.
The advantage of the DAO pattern is:
Its easy to define another style of persistence, eg moving from a Database to cloud, without changing the underlying impelementation, while the external interface remains the same, therefore not affecting other classes.
The persistence concerns are modularized away from the main model object concerns.
The advantage of the ActiveRecord pattern is simplicity.

- 28,258
- 22
- 102
- 185
-
1I think your first bullet is inaccurate. Perhaps in some implementations, but in others it's completely possible to change the persistence mechanism (even at runtime) relationdb, flat file, or web service without changing the interface... especially if that interface is well designed (i.e. it's just create, read, update, delete). The difference is that (again, depending on the implementation...as these traits can be mixed in with many languages...) – Mainguy Mar 02 '16 at 13:04
-
@Mainguy Yes, if the language provides effortless and dynamic (not static) mixins DAO goals could be met already by ActiveRecord. – Jasper Blues Nov 03 '17 at 09:10
-
I think it's worth noting that `ActiveRecord` is essentially a legacy pattern replaced by `DAO`. – Amir Abiri Aug 16 '18 at 14:40
-
2@AmirAbiri Who says `ActiveRecord` is essentially a legacy pattern? Many frameworks still use it. E.g., Ruby on Rails, Mongoose, Sequelize, Django, etc. – Danny Hurlburt Sep 18 '18 at 19:26
Data Access Object (DAO) refers to an object in your data layer responsible for persisting a separate entity in your domain. Active Record is a specific method of doing a DAO where the class containing the values of a single row from a table is also responsible for queries, updates, inserts, and deletes to that table. The Active Record design pattern means your object has a one-to-one mapping with a table in your database.

- 17,595
- 7
- 83
- 103