返回值,用于在未找到值时更新 DAL 中的数据

发布于 2024-12-25 21:13:19 字数 276 浏览 5 评论 0原文

我知道这是一个挑剔的问题,但我很好奇你们大多数人在这种情况下都在做什么。

我有一个 DAL,它在被调用时更新数据库记录。如果相关记录不存在,则有几种返回 BLL 的可能性。

1) 返回一个布尔值。 true 表示已更新, false 表示未更新。 2)返回异常,指示未找到要更新的值 3)返回受影响的行数...但是,当意图是单行更新时,这没有意义 4)返回一个字符串,表示“在数据库中找不到”(到目前为止最不喜欢的,但其他人已经在这里做过的事情)。

我倾向于1或2之间。你们对此有何看法?

I know this is a nit picky question, but I'm curious what the majority of you guys are doing in this situation.

I have a DAL that updates a database record when it is called. If the record in question does not exist, there are a few possibilities of how to return back to the BLL.

1) Return a boolean. True means it was updated, false means it was not.
2) Return an exception indicating value to be updated was not found
3) Return the number of rows affected...however this doesn't make sense when the intent is a single row update
4) return a string saying "not found in database" (least favorite by far, but something that others have done around here).

I'm leaning between 1 or 2. How do you guys feel about this?

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

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

发布评论

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

评论(2

梦初启 2025-01-01 21:13:19

我通常会这样做 2) 除非“未找到”条件不是例外情况。如果“未找到”相当常见,我通常会执行 1)。

I would usually do 2) unless the 'not found' condition is not an exceptional case. If 'not found' is fairly common, I will usually do 1).

固执像三岁 2025-01-01 21:13:19

需要考虑的事项:

  1. DAL 的其他部分的行为是什么?如果这些不同调用的行为方式具有一定的一致性,那就太好了。
  2. 语言是什么?它如何处理异常会影响我对 #2 的评估

,如果不知道其中任何一个,我会选择 #2:它不仅可以处理所描述的特定情况,还可以解决更新可能失败的其他原因/o 需要添加新机制。

Things to consider:

  1. What is the behavior of other parts of your DAL? It would be nice to have some consistency between how those different calls behave.
  2. What is the language? How it deals w/ exceptions would color my assessment of #2

W/o knowing either of these, I'd opt for #2: not only does it handle the particular situation described, but allows for addressing other reasons an update might fail w/o needing to add a new mechanism.

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