Handing exception in BLL and return to client (either winforms or webforms)?

偶尔善良 提交于 2019-12-20 05:23:33

问题


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?


回答1:


You can do several things;

  1. Focus on improving the user experience when an unexpected error presents itself.

  2. Always log errors either in eventlog or database.

  3. Implement sufficient infrastructure to not let exceptions happen unless they are system exceptions.

  4. Use throw instread of throw exception

Some links to help you:

  1. http://today.java.net/pub/a/today/2003/12/04/exceptions.html

  2. http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html

  3. http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx




回答2:


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);
  }
  ...
}


来源:https://stackoverflow.com/questions/800161/handing-exception-in-bll-and-return-to-client-either-winforms-or-webforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!