我需要服务对象吗? [英] Do I need a Service object?

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

问题描述

我正在开发课程总结工作(最终毕业作品). 我们仅将Java与Spring MVC和Glassfish Application Server一起使用.我们将不使用Hibernate或JPA,因为我们将在MS SQL Server 2008中使用存储过程,因此JDBC似乎是调用它们的最有效方法(据我所知,我不能像SQL Server的参数).我们已经在Glassfish上配置了数据源池.我们知道我们需要DAO才能从SQL Server调用过程.我的问题是:

I'm developing my Course Conclusion Work (Final Graduation Work). We're using Java with Spring MVC and Glassfish Application Server only. We won't use Hibernate or JPA because we'll use stored procedures in MS SQL Server 2008, so JDBC seems to be the most effective way to call them (since, as far as I know, I can't pass an object as a parameter to SQL Server). We've configured the Datasource Pool on Glassfish. We know we'll need a DAO to call the procedures from SQL Server. My questions are:

1-)我是否需要(或者最好是使用)Service对象(正确标注为@Service)来调用DAO方法?或者我可以直接从控制器调用DAO方法?

1-) Do I need (or is it the best practice to use) a Service object (properly annoted as @Service) to call the DAO methods? Or I can just call the DAO methods right from the controller?

2-)从数据源获取连接的最佳方法是什么?每个DAO上都有一个带有getConnection方法或ds.getConnection()的单独类?

2-) What's the best way to get the Connection from the DataSource? A separated class with a getConnection method or ds.getConnection() on each DAO?

谢谢

推荐答案

我倾向于拥有服务层的最大原因是以下两个原因...

The biggest reasons I tend to lean towards having a service layer are for the following two reasons...

  1. 将方法标记为@Transactional,因此该方法内的所有DAO请求都在单个事务中发生.

  1. Marking methods as @Transactional so all the DAO requests inside this method take place in a single transaction.

我可以增加关注点与DAO和用户角色的分离.我可以将方法标记为需要某些用户角色,因此在Spring Security中可以很好地满足要求.

I can increase the seperation of concern from the DAO and the user roles. I can mark methods as requiring certain user roles and as such it sits really nicely with Spring Security.

您可以只从控制器调用DAO,但是如果以后要扩展,它将使您陷入困境.要获得连接,可以使用将服务标记为@Transactional,将DAO标记为@ Repository,Spring将为您处理整个会话对象并为您注入对象.

You can just call a DAO from the controller, but it leaves you stuck if you want to expand later on. To get the connection you can use mark the service as @Transactional and the DAO as a @Repository and Spring will handle the whole getting the session object for you and injection etc.

我倾向于(可能是错误地)认为服务层是可以访问的外观,无论是从Web服务还是从控制器或其他任何东西,它都保持分隔.

I tend to think (maybe incorrectly) of the service layer as a facade that gets accessed, be it from a web service or a controller or whatever, it keeps seperation.

这篇关于我需要服务对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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