Difference between Active Record and DAO?

心不动则不痛 提交于 2019-12-22 01:25:30

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6640784/difference-between-active-record-and-dao

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!