且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有休眠的DAO和服务层

更新时间:2023-01-12 20:01:08

  1. 您正在使用 actionPerformed()方法处理事务.它明显违反了DAO/服务层的目的
  2. 您的 UserService 正在接受 AbstractDAO ,这意味着其他一些代码可能会将错误的DAO实现传递给您的 UserService ,这会使事情搞砸
  1. You are handling the transaction inside your actionPerformed() method. Its clearly defeating the purpose of DAO/Service layer
  2. Your UserService is accepting AbstractDAO, which means some other code may pass wrong DAO implementation to your UserService that will screw things up

现在,很少有建议.

  1. 您可以为此目的研究 GenericDAO概念.这可能适合您的需求
  2. 大多数时候,我们不需要所有这些层,例如 Service DAO BusinessDelegate .因此,问问自己是不是真的回答了您的某些问题.如果没有,请摆脱它们.YAGNI
  3. 完全摆脱DAO,将Hibernate API当作DAO.用您的业务方法处理数据库事务.您可能想阅读此问题
  1. You can look into GenericDAO concept for this. That might suit your need
  2. Most of the time we ain't need all these layers like Service, DAO and BusinessDelegate. So, question yourself are any of these really answering some of your questions. If not, get rid of them. YAGNI
  3. Get rid of DAO completely, and treat your Hibernate API as your DAO. Handle database transaction in your business methods. You may like to read this question

[已编辑]

编辑后,我的第3条建议没有多大用处.顺便说一下,您将DAO命名如下: UserJdbcDAO UserMysqlDAO 等.您的第二个名字意义不大,因为我们使用ORM只是为了避免数据库供应商特定的DAO/查询.如果您的 UserMysqlDAO扩展了UserJdbcDAO ,这可能会变得有些道理.

After your edit my 3rd suggestion doesn't carry much weight. By the way, you name your DAOs as follows; UserJdbcDAO, UserMysqlDAO etc. Your 2nd name is not making much sense, as we use ORMs just to avoid DB vendor specific DAOs/queries. It might start making some sense if your UserMysqlDAO extends UserJdbcDAO.