为数据库以外的数据源设计 DAO
到目前为止,我已经习惯使用 DAO 从数据库中检索信息。不过,其他数据源也是可能的,我想知道是否以及如何一般应用该模式。
例如,我现在正在开发一个在 Web 上获取 XML 的应用程序。 XML 文件可以被视为数据源,实际的获取原则上类似于数据库请求。但我不太确定 DAO 是如何构建的。
欢迎对这个主题有任何意见。
Until now I've been used to using DAOs to retrieve information from databases. Other sources of data are possible though and I'm wondering if and how the pattern could be applied in general.
For example, I'm now working on an application that fetches XML on the web. The XML file could be considered as a data source and the actual fetching is similar in principle to a database request. I'm not quite sure how the DAO could be structured though.
Any views on the subject are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
例如,请参阅此处的“将非数据库数据资源封装为 DAO 类”部分:
http:// /java.sun.com/blueprints/patterns/DAO.html
See for example "Encapsulating non-database data resources as DAO classes" section here:
http://java.sun.com/blueprints/patterns/DAO.html
由于 DAO 只用对象来表达 CRUD 操作,而没有引用它们的数据源,所以我不明白为什么这是一个问题。如果您的 DAO 从满足这些标准的接口开始,那么客户端不需要知道它是通过 XML 还是关系数据库实现的。
.NET 的 LINQ 成功地扭转了这一局面。也许您可以模仿另一种设计来解决这个问题。
Since DAOs only express CRUD operations in terms of objects, without ever referring to their data source, I can't see why this is a question. If your DAO begins with an interface that meets those criteria clients need not know whether or not it's implemented in terms of XML or a relational database.
.NET's LINQ manages to turn that trick. Maybe it's another design that you can emulate for this problem.
正如您已经说过的,您的 DAO 提供了独立于任何数据源的通用方法。因此,您创建一个 DAO 接口,然后提供不同的实现。其他类则仅使用 DAO 接口。
Your DAO offers generic methods that are - as you already said - independent of any datasource. Therefore you create a DAO interface and then just provide different implementations. Other classes then only use the DAO interface.