返回值,用于在未找到值时更新 DAL 中的数据
我知道这是一个挑剔的问题,但我很好奇你们大多数人在这种情况下都在做什么。
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常会这样做 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).
需要考虑的事项:
,如果不知道其中任何一个,我会选择 #2:它不仅可以处理所描述的特定情况,还可以解决更新可能失败的其他原因/o 需要添加新机制。
Things to consider:
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.