异常处理的分层体系结构 [英] Exception handling in layered architecture

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

问题描述

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

  1. 我们不希望暴露太多的信息从BLL到外面的世界。 (从其他层BLL是罚款)
  2. 我们不希望混乱的code与尝试捕捉栈
  3. 我们不想惹异常处理code(如日志,电子邮件等)的catch块

可能有人张贴一些code样品或文学的指针,我们可以用它来设计我们的简单的异常处理框架?

解决方案
  

我们不希望从BLL暴露过多信息,外面的世界。
  (从其他层BLL是罚款)

这是BLL本身定义什么是暴露无遗。请确保您能告诉什么打算待观察。

  

我们不想弄乱code与尝试捕捉栈

那么,别。例外情况也有例外。使用起来不控制流量。让他们炸毁。

  

我们不想惹异常处理code(如日志,电子邮件等)的catch块

如果您的逻辑不依赖于异常处理(它不应该)和您的 code卫士本身(这个是很重要的,你的应用程序应该始终吹,而不是进一步的工作,否则上无效状态 - 这是很难理解是什么原因导致什么),那么它的足够多的与包装整个应用程序只有1转储必要的堆栈跟踪,其中的错误处理。

例如。 - 在.NET中,可以使用订阅 AppDomain中未处理的异常事件为。

我个人使用 ELMAH 我的web应用程序 - 在app几行。配置和我有很好的错误日志,存储在sqlite的,从web应用程序本身很容易accessable。这是对我把所有的错误处理。

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. We don't want to expose too much information from BLL to outside world. (from other layers to bll is fine)
  2. We don't want to clutter the code with try catch stacks
  3. We don't want to mess the exception handling code(like logging, emailing etc) in catch blocks

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

解决方案

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

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

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.

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

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.

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

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天全站免登陆