从 DAL 捕获每个异常和另一个异常
我有一个 n 层 Web 应用程序,我想在来自 DAL(数据访问层)的每个方法中捕获特定类型的异常,并将其重新抛出为特定类型的新异常。
我的 DAL 中有很多方法,所以我不想开始用 try/catch 包装每个方法。
我认为使用异常处理应用程序块可以做到这一点,但我找不到任何关于如何执行此操作的良好文档...
我也不熟悉该应用程序块的早期版本。
I have an n-tier web application, and I want to catch a specific type of exception in every method coming from the DAL (Data access layer) and rethrow it as a new exception of a specific type.
There are many methods in my DAL, so I don't want to start wrapping each one with try/catch.
I think this is possible using the Exception Handling Application Blocks, but i couldn't find any good documentation of how to do this...
I'm not familiar with the previous versions of the application blocks neither.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 DAL 存储库有接口吗?
我将使用装饰器模式来实现该接口。
装饰器所做的所有工作都会捕获异常,然后构建一个新的异常并将其抛出到上层
值得注意的是,在我们的 n 层应用程序中,我们总是让异常自然地抛出,并在一个地方捕获它们并记录他们。我们只会在绝对必要的情况下创建特定的例外,而且这种情况很少见。
这样做的原因是代码的可维护性。当 try/catch 到处存在时,代码很容易变得不可读。
Has your DAL repositories got an interface?
I would implement the interface using a decorator pattern.
All the decorator does catches the exception and then builds a new exception and throws that out to the upper tier
As a point of note, in our n-tiered applications we always let exception just get thrown naturally and catch them in once single place and log them. We only create specific exceptions if we absolutely have to and that would be rare enough.
The reason for this is maintainability of code. Code can easily become unreadable when try/catches exist everywhere.