DAO和服务层设计 [英] DAO and Service layer design

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

问题描述

我正在使用Java EE 6开发Web应用程序.为了最大程度地减少对数据库的调用,拥有类是一个好主意:

I am developing web application with Java EE 6. In order to minimize calls to database will it be a good idea to have classes:

数据访问类(DAO)将仅调用基本方法getAllClients, getAllProducts, getAllOrders, delete, update方法-CRUD方法.

Data access class (DAO) will call only basic methods getAllClients, getAllProducts, getAllOrders, delete, update methods - CRUD methods.

服务类,它将调用CRUD方法,但另外还包含过滤器方法,例如findClientByName, findProuctByType, findProductByYear, findOrderFullyPaid/NotPaid等...,这将基于基本的DAO方法.

Service class which will call CRUD methods but in addition filter methods e.g. findClientByName, findProuctByType, findProductByYear, findOrderFullyPaid/NotPaid etc... which will be based on basic DAO methods.

谢谢

推荐答案

以我的经验(尽管有限),DAO类倾向于具有允许应用程序执行的所有可能的数据库操作.因此,根据您的情况,它将具有诸如getAllClients()getClientByName(String name)之类的方法.

In my experience (albeit, limited) DAO classes tend to have all the possible database operations which the application is allowed to perform. So in your case, it will have methods such as getAllClients() and getClientByName(String name), etc.

在DAO中获取所有用户并对其进行遍历,直到找到所需的用户为止,这将不必要地浪费计算时间和内存消耗.

Getting all the users in your DAO and iterating all over them until you find the one you need will result in unneeded waste of computational time and memory consumption.

如果要减少数据库命中的次数,可以实现某种缓存机制. ORM 框架,例如 Hibernate 应该能够提供您所需的信息,如此处所示.

If you want to reduce the amount of times that your database is hit you could, maybe, implement some caching mechanism. An ORM framework such as Hibernate should be able to provide what you need as shown here.

根据您的评论问题,不,您的服务将不会变得多余.通常要做的是使用Service层公开DAO功能.基本上,这将使DAO从应用程序的前端不可见.它通常还允许使用其他方法,例如public String getUserFormatted(String userName).这将利用DAO提供的getUserByName功能,但提供一些额外的功能.

As per your comment question, no, your service will not be made redundant. What one does is to usually use a Service layer to expose the DAO functionalities. This will, basically, not make the DAO visible from the from front end of your application. It usually also allows for extra methods, such as, for instance, public String getUserFormatted(String userName). This will make use of the getUserByName function offered by the DAO but provide some extra functionality.

Service层也将在规范更改时变得很有用,并且您现在还需要一个Web服务来与您的应用程序接口.介于两者之间的服务层将允许Web服务通过Service层查询DAO.

The Service layer will also make itself useful should there be a change in specification and you now also need a web service to interface with your application. Having a service layer in between will allow the web service to query the DAO through the Service layer.

因此,基本上,DAO层仍然会担心数据库内容(CRUD操作),而服务将调整DAO返回的数据而不会暴露DAO.

So basically, the DAO layer will still worry about the database stuff (CRUD Operations) while the service will adapt the data returned by the DAO without exposing the DAO.

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

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