为什么在一个持久层把一个DAO层(像JDO或休眠) [英] Why put a DAO layer over a persistence layer (like JDO or Hibernate)

查看:315
本文介绍了为什么在一个持久层把一个DAO层(像JDO或休眠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据访问对象(DAO的)是一种常见的设计模式,以及由Sun推荐但Java的DAO的最早的例子直接与关系数据库的交互 - 他们在本质上,这样做的对象关系映射(ORM)。现在,我看到的DAO像JDO和Hibernate成熟的ORM框架的顶部,我不知道这是否真的是一个不错的主意。

Data Access Objects (DAOs) are a common design pattern, and recommended by Sun. But the earliest examples of Java DAOs interacted directly with relational databases -- they were, in essence, doing object-relational mapping (ORM). Nowadays, I see DAOs on top of mature ORM frameworks like JDO and Hibernate, and I wonder if that is really a good idea.

我正在开发使用JDO作为持久层的Web服务,并正在考虑是否引进的DAO。与包含地图等对象的特定类打交道时,我可以预见到的一个问题:

I am developing a web service using JDO as the persistence layer, and am considering whether or not to introduce DAOs. I foresee a problem when dealing with a particular class which contains a map of other objects:

public class Book {
    // Book description in various languages, indexed by ISO language codes
    private Map<String,BookDescription> descriptions;
}

JDO是足够聪明的这个映射到书和BOOKDESCRIPTIONS表之间的外键约束。它透明加载BookDescription对象(使用延迟加载,我相信),并持续他们的时候Book对象被持久化。

JDO is clever enough to map this to a foreign key constraint between the "BOOKS" and "BOOKDESCRIPTIONS" tables. It transparently loads the BookDescription objects (using lazy loading, I believe), and persists them when the Book object is persisted.

如果我是引入了数据访问层,并写这样BookDao一类,并封装在这一切的JDO code,那么这是不是JDO的孩子的透明装载物体避开了数据访问层?为了保持一致性,不应该全部BookDescription对象被加载,并通过一些BookDescriptionDao对象(或BookDao.loadDescription方法)持续?然而,在这种方式重构将使操纵模型不必要的复杂。

If I was to introduce a "data access layer" and write a class like BookDao, and encapsulate all the JDO code within this, then wouldn't this JDO's transparent loading of the child objects be circumventing the data access layer? For consistency, shouldn't all the BookDescription objects be loaded and persisted via some BookDescriptionDao object (or BookDao.loadDescription method)? Yet refactoring in that way would make manipulating the model needlessly complicated.

所以我的问题是,什么是错调用JDO(或休眠状态,或任何ORM你喜欢)直接在业务层?它的语法已经相当简洁,它是数据存储无关。有什么优势,如果有的话,数据访问对象封装呢?

So my question is, what's wrong with calling JDO (or Hibernate, or whatever ORM you fancy) directly in the business layer? Its syntax is already quite concise, and it is datastore-agnostic. What is the advantage, if any, of encapsulating it in Data Access Objects?

推荐答案

这取决于你的图层的目标是什么。你把一个抽象了另一组以提供一组不同的语义。一般来说进一步层有简化出头,如未来maintennance的发展。但是,他们可以有其他用途。

It depends what your layer's goals are. You put an abstraction in to supply a different set of semantics over another set. Generally further layers are there to simplify somethings such as development of future maintennance. But they could have other uses.

例如一个DAO(或持久性处理)层上的ORM code电源专门的恢复和错误处理功能,你不想污染的业务逻辑。

For example a DAO (or persistence handling) layer over an ORM code supply specialised recovery and error handling functionality that you didn't want polluting the business logic.

这篇关于为什么在一个持久层把一个DAO层(像JDO或休眠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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