处理多层应用程序中的错误 - 建议(最佳实践?) [英] Handling Errors in Multi-Tier Application - Advice (best practices?)

查看:94
本文介绍了处理多层应用程序中的错误 - 建议(最佳实践?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个多层应用程序,简化,假设我有以下内容:



用户界面 - > BusinessLogicLayer - > DataAccessLayer - >数据库

- UserInterface:WPF

- 层之间的通信WCF

- 代码C#

- 框架4.0(VS2010) )

- SQL Server 2008数据库



我希望能够通过我的UI进行简单的查询,通过我的BLL,DAL ,DB并返回List< Something>回到所有层。到目前为止一直这么好......



我的问题:

让我们说BLL或DAL会出现某种错误(例如 - > ;我的一个参数是不一致的,这会抛出异常),我如何与所有其他图层通信以说"返回List< Something>的方法GetSomething()实际上返回了
一个错误:'无效参数'在某个地方的某个地方"?


我正在考虑创建一个包含我所有应用程序错误的枚举(由方式,我有一个包含所有Business Objects和Common类(如Enums)的项目,并将其保存在我的Common项目中。所以,通过这个,我的整个应用程序可以访问可能的错误...
$


但是我如何更改一个可能返回List的方法< Something>支持还返回MyAppplicationErrors。我希望显然可以使它尽可能通用。



那里有什么伟大的想法吗?我可以尝试其他任何方法吗?建议?



$


如有任何反馈,请提前致谢,
SuperJB 8 - )


JB

Hello everyone,

I have a multi-tier application, to simplify, lets say i have the following:

User Interface -> BusinessLogicLayer -> DataAccessLayer -> DBs
- UserInterface: WPF
- Communication between Layers WCF
- Code C#
- Framework 4.0 (VS2010)
- SQL Server 2008 DBs

I want to be able to do a simple query from my UI, go through my BLL, DAL, DB and return a List<Something> back through all layers. So far so good...

My problem:
Lets say some kind of error occurs in BLL or DAL (for example -> one of my params is inconsistent and this throws an Exception), how can i communicate with all other layers to say "the method GetSomething() that returns a List<Something> actually returned an Error: 'Invalid Parameter' somewhere along the line"?

I was thinking of creating an Enum of all my application Errors (by the way, i have a project with all my Business Objects and Common classes like Enums) and saving it in my Common project. So, with this, my entire application can access the possible Errors...

But how do i change a method that supposedly returns a List<Something> to support also returning MyAppplicationErrors. I want to make this as generic as possible obviously.

Any GREAT IDEAS out there? Any other approaches i can try? Advice?



Thanks in advance for any feedback,
SuperJB 8-)


JB

推荐答案

我会告诉你我在你所拥有的相同场景中完成了什么,你可以去就你所知,问题是我确定你不知道它是否值得。

Ill tell you what i have end done in the same scenario you have, you can go as far as u can, the problem is im sure you dont know if its worth it.

首先在我所有的业务逻辑类库和DB库(一般情况下)我没有捕获异常(我扔了很多)除非我真的需要它,因为处理有特殊的逻辑(比如内部数据库事务)。

First in all my bussiness logic class librarys and DB librarys (in general) i dont catch exceptions (i throw a lot) unless i really need it, because the handling has special logic (like inside DB transactions).

所有异常都在WCF服务中捕获,我的所有方法服务包括:

All the exceptions are catched in the WCF service, all the methods in my services are:

public  SomeDataContract DoSomeWork(string someStuff)

{

 试试¥b $ b   {

  返回BussinessFacade.DoSomeWork(someStuff);

 }

  catch(Exception ex)

  {

   // logger是来自log4net库的ILog

   logge r.Error(" Error en DoSomeWork",ex);

public SomeDataContract DoSomeWork(string someStuff)
{
 try
 {
  return BussinessFacade.DoSomeWork(someStuff);
 }
 catch (Exception ex)
 {
  //logger is a ILog from log4net library
  logger.Error("Error en DoSomeWork", ex);

  抛出新的FaultException(ex.Message);

 }

}

  throw new FaultException(ex.Message);
 }
}

最重要的是,即时捕获可以在调用中抛出的所有异常(除非你在BLL中启动新线程)。

The big points are, im catching all exceptions that can be thrown in the call (unless you starting new threads in BLL).

在服务器中,异常由日志库处理我使用壮观的

log4net
,也可以使用企业库日志记录。

In the server the exception is handled by a logging library i use the spectacular log4net, enterprise library logging could be used too.

客户端只收到错误的小文本详细信息。一个WCF通信专用类(FaultException)

The client only receive a small text detail of the error in a WCF communications specialized class (FaultException)

I USE DEBUG版本二进制文件没有发布,性能损失是最小的,在任何异常中我都有确切的代码行来解决问题。

I USE DEBUG version binarys not release, performance penalty is minimun and in any exception i have the exact lines of code where the problem raised.

使用lognet我可以配置向我发送错误的电子邮件,发送短信,只需登录文件或我需要的任何内容。

With lognet i can configure to send me email of the errors, send me sms, just log in a file or whatever i need.

如果你需要专门的错误指示你可以使用FaultException<>泛型类或事件标准的FaultCode属性,检查帮助或msdn中的FaultException类,是你在
中描述的问题的解决方案"我的问题:"

If you need specialized error indications you can use FaultException<> generic class or event the FaultCode property of the standar one, check in the help or in the msdn the FaultException class, is the solution to your problem you described in "My problem:"

玩得开心!


这篇关于处理多层应用程序中的错误 - 建议(最佳实践?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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