为DAO模式的最佳实践? [英] Best practice for DAO pattern?

查看:125
本文介绍了为DAO模式的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多的codeS使用服务-DAO模式,我不知道这种模式的由来。它迫使前层呼叫服务,然后委托一些服务任务来道。

我要问:


  1. 是否DAO层做纯粹的数据访问相关的任务?怎么样之类的东西外封装?

  2. 有没有可以用来替代这个或比这更好的其他模式?

  3. 我觉得POJO域模型和交易脚本,让即使是简单的问题变得复杂,是否有可能完全消除DAO层?


解决方案

理想情况下,DAO层抽象了'的访问某些数据存储系统(数据库,文件系统,LDAP目录,...)。所以在这个意义上它仅用于数据访问相关的任务。不过,你也可以拥有访问Web服务或其他一些组件外部的应用程序中的DAO层。这是关键点,它提供了访问一些外部组件。

主要的想法是,有逃脱给高层(隔离)你的DAO层没有实现细节。为思考这方面的一个很好的出发点是:什么我需要,如果我打算更换组件(例如数据库),我的DA​​O层提供访问呢?例如,你有XML文件里面的一些数据,并计划将数据迁移到一个数据库中。

假设你有各种逃脱你的DAO层与XML相关的异常。然后,它变得相当困难的XML层迁移到数据库层。不过,如果你已经封装的DAO层所有的实现细节,这将成为一个容易得多。

在最后,这是你的code的可维护性。你有一个特定层的实现细节越少依赖(服务,DAO,...),更好的维护您的code为。

I've seen a lot of codes use a service-dao pattern , I don't know the origin of this pattern . It force the front layer call service , then delegates some of the service task to dao.

I want to ask :

  1. Does DAO layer do purely data access related task ? What about things like exception encapsulation?
  2. Is there any other pattern can be used to replace this or better than this ?
  3. I think pojo domain models and transaction scripts make even simple problem became complicated , is it possible to completely eliminate dao layer ?

解决方案

Ideally, your DAO layer 'abstracts away' the access to some data storage system (database, file system, LDAP directory, ...). So in that sense it is used only for data access related tasks. However, you could also have a DAO layer that accesses a web service or some other component external to your application. That's the key point, it provides access to some external component.

The main idea is that there are no implementation details of your DAO layer that escape to higher layers (isolation). A good starting point for thinking about this is: what would I need to do if I plan to replace the component (a database for example) that my DAO layer provides access to? For example, you have some data inside XML files and you plan to migrate the data to a database.

Suppose you have all kinds of XML-related exceptions that escape your DAO layer. Then it becomes quite hard to migrate your XML layer to a database layer. However, if you've encapsulated all implementation details of your DAO layer, this will become a lot easier.

In the end, it's about maintainability of your code. The less dependencies you have on implementation details of a specific layer (services, DAO, ...), the better maintainable your code is.

这篇关于为DAO模式的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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