使用 DAO 模式的利弊 [英] Pros and cons of the use of the DAO pattern

查看:39
本文介绍了使用 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 按 z 排序".然后,该数据在下拉列表中使用,但通常仅用于键/值组合,忽略检索和映射的对象中的所有其他内容(创建的数据、最后更新它的用户、它是否处于活动状态等).即使这种按摩发生在 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 不能设计为检索参考数据的 Map——它当然可以.但通常它们用于完整的对象映射,这并不是一直需要的.这是保存时的优势,但 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天全站免登陆