逻辑应该在哪里来决定执行哪个 SQL 查询

发布于 2024-10-13 17:47:18 字数 636 浏览 2 评论 0原文

我有一个 DAO,其方法 CommitmentListDAO.getListByOwnerBadge 返回针对主管徽章的承诺项目数组列表(数据库字段 OWNED_BY

    String SQL_VIEW_LIST_BY_SUPERVISOR = SELECT_QUERY + 
    " WHERE c.OWNED_BY = ? " +
    " ORDER BY p.PROGRAM_NAME";

现在,我想添加一个下拉菜单我的网络表单允许用户在 Owned ByTasked To 之间进行选择 我需要在 DAO 中添加一个 WHERE c.TASKED_TO = ? 子句。

我是否执行在 DAO 中搜索哪个字段的逻辑 - 比如说传入的下拉参数(不是请求对象)并将方法重命名为 getListByBadge(String WhichField, String bad) 或我的 CommitmentListForm 类应该具有此逻辑,然后对 getListByOwnerBadgegetListByTaskeToBadge 进行适当的调用

I have a DAO with a method CommitmentListDAO.getListByOwnerBadge that returns an arraylist of commitment items against a supervisor badge (database field OWNED_BY)

    String SQL_VIEW_LIST_BY_SUPERVISOR = SELECT_QUERY + 
    " WHERE c.OWNED_BY = ? " +
    " ORDER BY p.PROGRAM_NAME";

Now, I want to add a pull down on my web form to allow the user to choose between Owned By or Tasked To
I'll need to add a WHERE c.TASKED_TO = ? clause in the DAO.

Do I perform the logic for which field to search on within the DAO - say a passed in parameter of the pulldown (Never the request object) and rename the method to getListByBadge(String whichField, String badge) or should my CommitmentListForm class have this logic and then make the appropriate call to either getListByOwnerBadge or getListByTaskeToBadge

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这样的小城市 2024-10-20 17:47:18

我会在 DAO 上使用两种不同的方法来清楚地区分调用的作用。

DAO 的要点是隐藏 SQL 实现细节。您应该始终从“如果我切换到不同的持久性机制(例如 HBase)怎么办?”的角度来考虑这样的问题。 HBase 实现可能不会以简单通过字段名称区分的方式存储它。 DAO 应该能够隐藏该细节,从而隐藏不同的方法。

当然,这只是我的意见。 :)

I would go with a DAO on it with two different methods to clearly differentiate what the call does.

The point of a DAO is to hide the SQL implementation details. You should always consider a question like this from the standpoint of, "What if I switched to a different persistence mechanism, like HBase?" The HBase implementation may not store this in a way that simply differentiates by a field name. The DAO should make it possible to hide that detail, thus the different methods.

Just my opinion, of course. :)

千年*琉璃梦 2024-10-20 17:47:18

我将确定控制器中的逻辑,并将两个 SQL 查询分成 getListByOwnerBadgegetListByTaskeToBadge。这样你就可以避免使用一种“包揽一切”的方法,并且很快就会失控。此外,选择使用“全部执行”方法的其他开发人员必须检查该方法的内部结构,以查看可以传入哪些有效字符串,而显式方法调用则可以清楚地了解该方法正在完成的任务。

I would determine the logic in your controller and separate the two SQL queries into getListByOwnerBadge or getListByTaskeToBadge. That way you avoid having a method that "does it all" and could quickly get out of hand. Furthermore, other developers who choose to use the "does it all" method will have to inspect the method's internals to see what valid Strings can be passed in, whereas an explicit method call makes it obvious as to what the method is accomplishing.

少年亿悲伤 2024-10-20 17:47:18

我认为第二种解决方案更好。使 DAO 尽可能简单。没有逻辑,没有标志。创建 2 个简单的方法并以形式决定调用哪一个。或者甚至在表单和 DAO 之间创建另一个层来决定调用哪个 DAO 方法。

I think that the second solution is better. Keep DAO as simple as it is possible. No logic, no flags. Create 2 simple methods and make decision in form which one to call. Or even create yet another layer between form and DAO that decides which DAO method to call.

隔纱相望 2024-10-20 17:47:18

总结一下:

来自 @McStretch - 调用的逻辑位于您的控制器中。

来自 @rfreak - 查询方法本身位于 DAO 中

这是一个示例:

//Controller
CommitmentListAction {

updateForm(...){ 
  List<CommitmentItem> commitmentItems;
  if (formUsesOwnedBy){
    commitmentItems = CommitmentItemDAO.getListByOwnerBadge(...);
  } else {
    commitmentItems = CommitmentItemDAO.getListByTaskeToBadge(...);
  }
  // Do stuff with commitmentItems.
}
// DAO

getListByOwnerBadge(...){
 String SQL_VIEW_LIST_BY_SUPERVISOR = SELECT_QUERY + 
    " WHERE c.OWNED_BY = ? " +
    " ORDER BY p.PROGRAM_NAME"
  return doQuery(SQL_VIEW_LIST_BY_SUPERVISOR); // Performs the actual query
}

getListByTaskeToBadge(...){
String SQL_VIEW_LIST_BY_TASKED_TO = SELECT_QUERY + 
    " WHERE c.TASKED_TO = ? " +
    " ORDER BY p.PROGRAM_NAME"
  return doQuery(SQL_VIEW_LIST_BY_TASKED_TO); // Performs the actual query
}

如果您要拥有许多不同的 CommitmetItems 视图或许多不同的条件,请考虑将条件传递给 DAO。但只有当出现过多的 getListBy[blah] 方法污染 DAO 时才执行此操作。

To summarize:

From @McStretch - The logic for which to call goes in your controller.

From @rfreak - The query methods themselves go in the DAO

Here's an example:

//Controller
CommitmentListAction {

updateForm(...){ 
  List<CommitmentItem> commitmentItems;
  if (formUsesOwnedBy){
    commitmentItems = CommitmentItemDAO.getListByOwnerBadge(...);
  } else {
    commitmentItems = CommitmentItemDAO.getListByTaskeToBadge(...);
  }
  // Do stuff with commitmentItems.
}
// DAO

getListByOwnerBadge(...){
 String SQL_VIEW_LIST_BY_SUPERVISOR = SELECT_QUERY + 
    " WHERE c.OWNED_BY = ? " +
    " ORDER BY p.PROGRAM_NAME"
  return doQuery(SQL_VIEW_LIST_BY_SUPERVISOR); // Performs the actual query
}

getListByTaskeToBadge(...){
String SQL_VIEW_LIST_BY_TASKED_TO = SELECT_QUERY + 
    " WHERE c.TASKED_TO = ? " +
    " ORDER BY p.PROGRAM_NAME"
  return doQuery(SQL_VIEW_LIST_BY_TASKED_TO); // Performs the actual query
}

If you're going to have many different views of the CommitmetItems or many different criteria, consider passing in the criteria to the DAO. But only do this at the time when it appears there is an excessive number of getListBy[blah] methods polluting the DAO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文