处理BLL中的异常并返回给客户端(winforms或webforms)? [英] Handing exception in BLL and return to client (either winforms or webforms)?

查看:159
本文介绍了处理BLL中的异常并返回给客户端(winforms或webforms)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找做异常处理的最佳方法,例如,当业务逻辑层发生错误时,是使用catch并且将EVENT返回到表示层来停止METHOD的最佳方法? / p>

这个事件应该包含什么?



还是应该总是BUBBLE异常并在表现层处理它们?



任何人都有一些良好的链接,并且需要阅读关于处理异常以及如何在客户端处理它们的最佳方式的阅读...



例如,如果我在BLL中得到一个NullException,我可以抓住这个..但是最好的方法是什么,并返回到presentaiton层,并通知它的问题..



事件?或者另一个尝试/ Catch in the presentation?

解决方案

有几种方法可以实现:



1)通过描述消息来抛出异常。



2)点火事件



3)使用特殊接口与用户进行交互。例如,您可以实现类似IUiCallbacks接口的接口,并将实现该接口的对象发送到BLL类或方法。后来,BLL中的方法可以调用IUiCallbacks.SendMessage()或IUiCallbacks.SendError()来通知演示。你可以有不同的类,如WinFormsUiCallbacks,WebFormsUiCallbacks和SilentUiCallbacks,实现这个界面。



我通常使用1)和3)



3)根据请求的示例:

  public interface IUiCallbacks 
{
void SendMessage(string message);
void SendException(string message,Exception ex);
}

public class WinFormsUiCallbacks:IUiCallbacks
{
public void SendMessage(string message)
{
MessageBox.Show(message);
}

public void SendException(string message,Exception ex)
{
MessageBox.Show(string.Format(不幸的是,发生以下错误:{ 0} {1},Environment.NewLine,ex.Message));
}
}

public class OrderService
{
private IUiCallbacks _iUiCallbacks;
...
public OrderService(){...}
public OrderService(IUiCallbacks iUiCallbacks)
{
_iUiCallbacks = iUiCallbacks;
}
...
public void AddOrder(Order order)
{
...
if(OrderAlreadyExists(order))
{
if(_iUiCallbacks!= null)
_iUiCallbacks.SendMessage(该命令无法添加,因为它已被接受。
返回;
}
...
}
...
}

所以可以这样使用:

  public partial class OrderForm:Form 
{
...
public void btnAddOrderFromExcel_Click(...)
{
订单订单= LoadOrderFromExcel(...);
OrderService orderService = new OrderService(new WinFormsUiCallbacks());
orderService.AddOrder(order);
}
...
}


I am looking for the best way to do exception handling, for example.. when an error occurs in the business logic layer, is the best way to stop the METHOD using a catch and return an EVENT to the presentation layer?

What should this event contain?

Or should i always BUBBLE up exceptions and handle them in the presentation layer?

Anyone have some good links and required reading on this with regards to the best way of handling exceptions and how to handle them in the client ...

For example if i get a NullException in the BLL than i can catch this.. but whats the best way and return to the presentaiton layer and informing it of the issue..

Event? or another try / Catch in the presentation?

解决方案

There are several ways to do it:

1) Throwing exceptions with describing message inside.

2) Firing events

3) Using special interfaces to interact with the user.
For example you can implement something like IUiCallbacks interface and send the object, implementing this interface, to the BLL class or method. Later, method in BLL can call IUiCallbacks.SendMessage() or IUiCallbacks.SendError() to notify presentation. And you can have different classes, such as WinFormsUiCallbacks, WebFormsUiCallbacks and SilentUiCallbacks, implementing this interface.

I usually use 1) and 3)

Example of 3) as requested:

public interface IUiCallbacks
{
  void SendMessage(string message);
  void SendException(string message, Exception ex);
}

public class WinFormsUiCallbacks : IUiCallbacks
{
  public void SendMessage(string message)
  {
    MessageBox.Show(message);
  }

  public void SendException(string message, Exception ex)
  {
    MessageBox.Show(string.Format("Unfortunately, the following errror has occurred:{0}{1}", Environment.NewLine, ex.Message));
  }
}

public class OrderService
{
  private IUiCallbacks _iUiCallbacks;
  ...
  public OrderService() { ... }
  public OrderService(IUiCallbacks iUiCallbacks)
  {
    _iUiCallbacks = iUiCallbacks;
  }
  ...
  public void AddOrder(Order order)
  {
    ...
    if(OrderAlreadyExists(order))
    {
      if(_iUiCallbacks != null)
        _iUiCallbacks.SendMessage("The order can not be added, because it is already accepted.");
      return;
    }
    ...
  }
  ...
}

So it can be used like this:

public partial class OrderForm : Form
{
  ...
  public void btnAddOrderFromExcel_Click(...)
  {
    Order order = LoadOrderFromExcel(...);
    OrderService orderService = new OrderService(new WinFormsUiCallbacks());
    orderService.AddOrder(order);
  }
  ...
}

这篇关于处理BLL中的异常并返回给客户端(winforms或webforms)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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