使用DAO模式的优点和缺点 [英] Pros and cons of the use of the DAO pattern

查看:417
本文介绍了使用DAO模式的优点和缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在标题中提到的,我很想知道你(经验丰富的开发人员)对DAO模式的使用有何看法,特别是在Web应用程序中。您发现了哪些优势及其使用后果有什么不受欢迎?

As I mention in the title, I'm interested to know what you (as experienced developers) think about the use of the DAO pattern, specifically within a web application. What advantages have you found and what consequences of its use have you disliked?

推荐答案

我看到的DAO问题是他们通常一直处理完整的对象。这会产生完全不需要的开销,而简单查询则不会存在这种开销。例如,如果要从数据库引用数据创建下拉列表,则DAO用户可以简单地说:获取此表的对象集合,其中y按y排序。然后,该数据用于下拉列表,但通常仅用于键/值组合,忽略已检索和映射的对象中的所有其他内容(创建的数据,更新它的最后一个用户,无论是否处于活动状态等) 。即使这种按摩发生在DAO调用附近并且对象在检索时也不会被存储(通常情况并非如此),不幸的是,对象通常包含在ac:forEach(JSP)中并迭代生成下拉列表),它仍会产生不必要的数据库和网络开销,更不用说保存这些对象的内存暂时增加。

The problems with DAOs that I have seen is that they typically handle full objects all the time. This creates completely unneeded overhead that wouldn't exist with simple queries. For example, if a drop down is to be created off of database reference data, a DAO user may simply say: "Get me the collection of objects for this table where y ordered by z". Then, that data is used in the dropdown, but typically only for a key/value combination, ignoring everything else in the objects (created data, last user who updated it, whether or not it is active, etc) that was retrieved and mapped. Even if this massaging happens near the DAO call and the objects do not get stored as they are retrieved (which is typically not the case, unfortunately, the objects are often wrapped in a c:forEach (JSP) and iterated over to produce a drop down), it still creates unneeded database and network overhead, not to mention the temporary increase in memory to hold these objects.

现在,这并不是说DAO可以设计用于检索参考数据的地图 - 当然可以。但通常它们用于完整的对象映射,这不是一直需要的。这是保存时的优势,但IMO在检索数据时是一个弱点 - 当然,你得到了所有这些 - 但通常你不需要全部,而且只会浪费内存,带宽和时间。

Now, this is not to say that a DAO can't be designed to retrieve a Map of reference data - it certainly can. But typically they're used for the full object mapping, which is not what is needed all the time. It is a strength when saving, but a weakness, IMO, when retrieving data - sure, you get all of it - but often you don't need all of it, and it just wastes memory, bandwidth and time.

这篇关于使用DAO模式的优点和缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆