了解服务层和 DAO 层 [英] Understanding Service and DAO layers

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

问题描述

我被要求在项目的业务逻辑模块中创建类的文档.我注意到有一个关于如何创建类的模式.图案是这样的

I was asked to create documentation of classes in the business logic module of a project. I noticed that there was a pattern on how the classes where created. The pattern looks like this

    public class AModel(){
          //fields
          //getter and setters
    }

    public class AService(){

          public void processA(AModel model){
                  //creates instance of AModel, assigns values to fields
                  //calls ADaoService methods
          }  

    }

    public class ADaoService(){

           //has methods which call ADao methods
           //sample
           public AModel retrieveById(long id){
                 log.debug(...);
                 return (ADao.retrieveById(id));
           }

    }

    public class ADAo(){
            //has entityManager and some query

            public AModel retrieveById(long id){
                  return((AModel) entityManager.find(AModel.class, id));
            }
    }

我不明白的是为什么 AService 调用 ADaoService 方法而不是只调用 ADao 方法,因为 ADaoService 方法只是调用 ADao 方法.在我看来 ADaoService 只是浪费代码.它们是使用 Hibernate 和 JBoss 服务器.我刚接触这种类型的架构.希望有人能帮我理解.谢谢.

What I don't understand is why does AService calls ADaoService methods instead of just calling ADao methods since ADaoService methods are just calling ADao methods. It seems to me that ADaoService was just a waste of code. They are usign Hibernate and JBoss server. I'm just new to this type of architecture. Hope someone can help me understand. Thanks.

推荐答案

好吧,如果 ADAoService 什么都不做,只是将调用委托给 ADAo 那么显然你是对的 -它没有存在的理由目前.

Well, if ADaoService is doing nothing but delegating calls to ADao then clearly you're right - it has no existence justification at the moment.

关于未来的理由,嗯,AFAIK,典型的分层不包括ADAoService层.我工作的地方没有.从未在 Hibernate 文档中见过它...

Regarding future justifications, well, AFAIK, the typical layering does not include ADaoService layer. Where I work we don't have it. Never seen it in Hibernate docs...

要么你的架构师对层很慷慨,要么他们脑子里有一些非典型的场景.

Either your architects were generous with layers or they had some non-typical scenario in mind.

如果该层没有当前用途,也没有明确的未来用途 - 最好不要使用它.

If there's no current usages of the layer and no clear future usages - you're better off without it.

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

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