DAO和服务层(JPA /休眠+春) [英] DAO and Service layers (JPA/Hibernate + Spring)

查看:102
本文介绍了DAO和服务层(JPA /休眠+春)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计基于JPA /休眠,Spring和Wicket的一个新的应用。在DAO层和业务层之间的区别不是很清楚,我虽然。根据维基百科,DAO是

I'm designing a new app based on JPA/Hibernate, Spring and Wicket. The distinction between the DAO and Service layers isn't that clear to me though. According to Wikipedia, DAO is

这提供了一个抽象的对象
  接口的一些类型的数据库或
  持久性机制,提供了一些
  不暴露具体操作
  数据库的信息。

an object that provides an abstract interface to some type of database or persistence mechanism, providing some specific operations without exposing details of the database.

我想知道一个DAO是否可能包含真的没有做太多的数据访问方法,但使用的查询是比较容易的方式执行?例如,得到的一组特定的机场运行所有航空公司的名单?这听起来对我来说更多的是服务层的方法,但我不知道,如果在服务层使用JPA EntityManager的是一个良好做法的例子吗?

I was wondering whether a DAO could contain methods that don't really have to do much with data access, but are way easier executed using a query? For example "get a list of all airlines that operate on a certain set of airports"? It sounds to me to be more of a service-layer method, but I'm not sure if using JPA EntityManager in the service layer is an example of good practice?

推荐答案

一个DAO应该提供访问单一的相关的数据源,并根据多么复杂的商业模式,将返回要么全成熟的业务对象,或简单的数据对象。无论哪种方式,DAO方法应该反映数据库有些严密。

A DAO should provide access to a single related source of data and, depending on how complicated your business model, will return either full fledged Business objects, or simple Data objects. Either way, the DAO methods should reflect the database somewhat closely.

一个服务可以提供更高级别的接口,不仅处理你的业务对象,但要获得在第一时间对它们的访问。如果我从服务获取的业务对象,该对象可以从不同的数据库中创建(和不同的DAO的),它可以从HTTP请求中提出的信息加以装饰。它可具有几个数据对象转换成一个单一的,强大的,业务对象一定的业务逻辑。

A Service can provide a higher level interface to not only process your business objects, but to get access to them in the first place. If I get a business object from a Service, that object may be created from different databases (and different DAO's), it could be decorated with information made from an HTTP request. It may have certain business logic that converts several data objects into a single, robust, business object.

我一般创建一个DAO认为它会被任何人谁去使用该数据库中使用,或设置业务相关的数据,这是字面上除了内部的触发器,函数和存储过程的最低水平code数据库。

I generally create a DAO thinking that it will be used by anyone who is going to use that database, or set of business related data, it is literally the lowest level code besides triggers, functions and stored procedures within the database.

特定问题的答案:

我不知道是否能DAO
  包含没有真正有方法
  做多的数据访问,但
  使用查询的方式更容易执行?

I was wondering whether a DAO could contain methods that don't really have to do much with data access, but are way easier executed using a query?

在大多数情况下没有,你想在你的服务层的更复杂的业务逻辑,从单独的查询数据的组装。不过,如果你担心的处理速度,服务层,即使它打破了模型的美感,在大致相同的方式,一个C ++程序员可以编写汇编code,加快委托给DAO的动作某些动作。

for most cases no, you would want your more complicated business logic in your service layer, the assembly of data from separate queries. However, if you're concerned about processing speed, a service layer may delegate an action to a DAO even though it breaks the beauty of the model, in much the same way that a C++ programmer may write assembler code to speed up certain actions.

这听起来对我来说更多的是
  服务层的方法,但我不知道
  如果在使用JPA的EntityManager
  服务层是很好的一个例子
  练?

It sounds to me to be more of a service-layer method, but I'm not sure if using JPA EntityManager in the service layer is an example of good practice?

如果你打算用你的实体管理器在你的服务,再联想到实体管理器为你的DAO的,因为这是它到底是什么。如果您需要删除一些多余的查询构建,不要在服务类这样做,提取到用于实体管理器,让你的DAO类。如果您的使用情况是非常简单的,你可以完全跳过服务层,并使用你的实体管理器,或DAO中的控制器,因为所有的服务要做的是转嫁给 getAirplaneById电话()来DAO的 findAirplaneById()

If you're going to use your entity manager in your service, then think of the entity manager as your DAO, because that's exactly what it is. If you need to remove some redundant query building, don't do so in your service class, extract it into a class that utilized the entity manager and make that your DAO. If your use case is really simple, you could skip the service layer entirely and use your entity manager, or DAO in controllers because all your service is going to do is pass off calls to getAirplaneById() to the DAO's findAirplaneById()

更新 - 要对于下面的讨论中澄清,在服务中使用的实体管理器很可能不是在大多数情况下也有因各种原因DAO层在评论中强调了最好的决定。但在我看来,这将是完全合理的假设:

UPDATE - To clarify with regard to the discussion below, using an entity manager in a service is likely not the best decision in most situations where there is also a DAO layer for various reasons highlighted in the comments. But in my opinion it would be perfectly reasonable given:


  1. 的服务需要不同的数据集互动

  2. 数据
  3. 在至少一组已经有一个DAO

  4. 服务类驻留需要一些持久性这是很简单的不模块中保证它自己的DAO

例如

//some system that contains all our customers information
class PersonDao {
   findPersonBySSN( long ssn )
}

//some other system where we store pets
class PetDao {
   findPetsByAreaCode()
   findCatByFullName()
}

//some web portal your building has this service
class OurPortalPetLostAndFoundService {

   notifyOfLocalLostPets( Person p ) {
      Location l = ourPortalEntityManager.findSingle( PortalUser.class, p.getSSN() )
        .getOptions().getLocation();
      ... use other DAO's to get contact information and pets...
   }
}

这篇关于DAO和服务层(JPA /休眠+春)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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