如何以及在何处处理 3 层 Web 应用程序中的异常?特别是Sql数据库异常
我正在构建标准的 3 层 ASP.NET Web 应用程序,但我正在努力解决在哪里执行某些操作 - 特别是处理异常。
我尝试在网上查找一些示例,但找不到任何示例来展示所有内容如何链接在一起的整个项目。
在我的数据层中,我连接到 SQL Server 并执行一些操作。我知道我需要捕获可能因此引发的异常,但我不确定在哪里执行此操作。
根据我读到的内容,我应该在 UI 层执行此操作,但在这种情况下,我不确定如何确保关闭与数据库的连接。有人能够澄清如何做到这一点吗?另外,如果有人知道我在哪里可以找到遵循最佳实践的示例 3 层 Web 应用程序,那也很棒。
谢谢
I'm building the standard 3-tier ASP.NET web application but I'm struggling as to where to do certain things - specifically handling exceptions.
I've tried to have a look around on the web for some examples but can't find any which go as far as a whole project showing how everything links together.
In my data-tier I'm connecting to SQL Server and doing some stuff. I know I need to catch exceptions that could be raised as a result but I'm not sure where to do it.
From what I've read I should be doing it in the UI tier but in that case I'm not sure how to ensure that the connection to the database is closed. Is anyone able to clarify how to do this? Also if anyone knows as to where I could find an example 3-tier web application that follows best practices that would be great too.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有简单的答案或模式可以确保成功。就像您的验证策略一样,您的确切异常处理策略特定于您的具体情况,并且通常是时间和全面性之间的权衡。不过,我们可以提供一些好的建议:
There are no easy answers or patterns that will ensure success. Just like your validation strategy, your exact exception-handling strategy is specific to your exact situation, and is often a trade-off between time and comprehensiveness. There is some good advice we can give though:
只是一个可能引导您思考的侧面:如果您有任何类型的卷可能导致并发问题(死锁),您将希望您的应用程序检测到特定的 SQL 错误并重试操作(例如事务)。这就需要在数据层或业务层进行一些异常处理。
-克里普
Just a side point that may steer your thinking: if you have any type of volume that may result in concurrency issues (deadlocks) you will want your application to detect that particular SQL error and retry the operation (e.g. transaction). That argues for some exception handling at either the data or business tiers.
-Krip
我相信最好的做法是在最后负责任的时刻处理异常。这通常意味着在 UI 级别(即 MVC 应用程序中的控制器或传统 asp.net 应用程序中的代码隐藏中)。在这个高层次上,您的代码“知道”用户的要求,以及如果某些功能不起作用需要做什么。
处理调用堆栈下部的异常通常会导致调用代码无法正确处理异常情况。
在数据层中,您将使用标准模式(例如
使用
语句用于IDisposables,例如SqlConnection),记录您知道可能发生的异常(对于内存不足或其他罕见情况,不要这样做),然后让当它们这样做时,它们会沿着调用堆栈向上流动。在调用者可能必须处理许多异常的情况下,您最多可能希望捕获这些异常并将它们包装在单个异常类型中。如果您需要在释放异常之前“清理”内容,则始终可以使用
finally
块 进行清理。您不必catch
才能使用finally
块。抱歉,我不知道有任何旨在突出正确的异常处理的小型网站的示例。
I believe its best practice to handle exceptions at the last responsible moment. This usually means at the UI level (i.e., the Controller in a MVC app or in the codebehind in a traditional asp.net app). Its at this high level that your code "knows" what the user is asking, and what needs to be done if something doesn't work.
Handling exceptions lower down in the call stack usually results in calling code not being able to handle exceptional situations properly.
In your data tier, you would use the standard patterns (e.g., the
using
statement for IDisposables such as the SqlConnection), document exceptions you know may occur (don't do this for out of memory or other rare cases), and then let them flow up the call stack when they do. At the most you might want to catch those exceptions and wrap them in a single exception type, in situations where MANY exceptions may have to be handled by callers.If you need to "clean up" stuff before letting exceptions go, you can always use a
finally
block to clean up. You don't have tocatch
in order to use afinally
block.I don't know of any examples of small websites which are designed to highlight proper exception handling, sorry.
这不是您问题的具体答案,但如果您对最佳实践感兴趣,我会查看 Microsoft 模式和实践:
应用程序体系结构指南
Web 应用程序指南
This is not a specific answer to your question but if you are interested in best practices I would look at Microsoft Patterns and Practices:
Application Architecture Guide
Web Applications Guides