我被要求在项目的业务逻辑模块中创建类的文档。我注意到有一个关于如何创建类的模式。图案看起来像这样
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 服务器。我只是这种架构的新手。希望有人能帮我理解。谢谢。
最佳答案
好吧,如果 ADaoService
除了将调用委托(delegate)给 ADao
什么都不做,那么显然你是对的 - 目前.
关于 future 的理由,好吧,AFAIK,典型的分层不包括 ADaoService
层。我工作的地方没有。在 Hibernate 文档中从未见过它...
要么您的架构师对层很慷慨,要么他们考虑了一些非典型场景。
如果该层没有当前用途并且没有明确的 future 用途 - 最好不要使用它。
关于java - 了解服务和 DAO 层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11981323/