分层架构中的异常处理 [英] Exception handling in layered architecture

查看:38
本文介绍了分层架构中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在分层设计中重构(当然也重新设计)我们的服务.我们有服务操作层(BLL)、网络抽象层 ->(处理网络代理)、数据抽象层.但是我们对我们的异常处理策略有点困惑.

We are Refactoring (and of-course redesigning) our Services in layered design. We have Service operations layer (BLL), Network abstraction layer -> (deals with network proxy), Data abstraction layer. But we are a bit baffled about our exception handling strategy.

  1. 我们不想从 BLL 向外界公开太多信息.(从其他层到bll就可以了)
  2. 我们不想用 try catch 堆栈弄乱代码
  3. 我们不想在 catch 块中弄乱异常处理代码(如日志记录、电子邮件等)

有人可以发布一些代码示例或参考文献,我们可以使用它们来设计我们简单的异常处理框架吗?

Could someone post some code samples or literature pointers which we can use to design our simple exception handling framework?

推荐答案

我们不想将太多来自 BLL 的信息暴露给外界.
(从其他层到bll就可以了)

We don't want to expose too much information from BLL to outside world.
(from other layers to bll is fine)

BLL 本身定义了公开的内容.确保你展示了你想要看到的东西.

It's BLL itself that defines what's exposed. Make sure You show what's intended to be seen.

我们不想用 try catch 堆栈弄乱代码

We don't want to clutter the code with try catch stacks

那就不要了.例外就是例外.不要使用它们来控制流量.让他们炸毁.

Then don't. Exceptions are exceptions. Don't control flow using them. Let them blow up.

我们不想在 catch 块中弄乱异常处理代码(如日志记录、电子邮件等)

We don't want to mess the exception handling code(like logging, emailing etc) in catch blocks

如果您的逻辑不依赖异常处理(它不应该依赖)并且您的代码保护自己(这一点很重要,您的应用程序应该总是在无效状态下爆炸而不是进一步工作.否则 - 很难理解是什么原因造成的),那么用包装整个应用程序就足够了只有 1 个错误处理程序在必要时转储堆栈跟踪.

If Your logic does not rely on exception handling (which it should not) and Your code guards itself (this one is important, Your application should ALWAYS blow up on invalid state instead of working further. otherwise - it's hard to understand what causes what), then it's more than enough with wrapping whole app with only 1 error handler that dumps stack trace where necessary.

例如- 在 .net 中,您可以使用订阅 appdomain 未处理异常事件.

E.g. - in .net, You can use subscribing to appdomain unhandled exception event for that.

我个人将 ELMAH 用于我的网络应用程序 - app.config 中的几行和我有很好的错误日志,存储在 sqlite 中,可以从 Web 应用程序本身轻松访问.这就是我得到的所有错误处理.

I personally use ELMAH for my web application - few lines in app.config and I have nice error log, stored in sqlite, easily accessable from web app itself. That's about all error handling I got.

这篇关于分层架构中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆