简体   繁体   English

使用接口的 UI BLL 通信

[英]UI BLL communication using interfaces

Found this question that explained a way to communicate between layers.发现这个问题解释了层间通信的方式。 However there is one more aspect.然而还有一方面。

Assuming I am using interfaces to send messages from the BLL to the UI as seen in the folowing example:假设我使用接口将消息从 BLL 发送到 UI,如以下示例所示:

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

Instead of the information message I want a confirmation box that would confirm the overwrite for the existing order.我想要一个确认框来确认覆盖现有订单,而不是信息消息。

How can I handle the result of the confirmation box in this case?在这种情况下,我该如何处理确认框的结果?

Thanks, Alex谢谢,亚历克斯

Simply make your SendMessage return DialogResult, and add all necessary parameters:只需让您的SendMessage返回 DialogResult,并添加所有必要的参数:

  public DialogResult SendMessage(string message, string caption, MessageBoxButtons buttons)
  {
    return MessageBox.Show(message, caption, buttons);
  }

And use it.并使用它。

If you don't want to reference DialogResult type in your IUiCallbacks interface then create your own DialogResult types and provide code to map (for example cast) between WPF DialogResult and your DialogResult.如果您不想在 IUiCallbacks 接口中引用 DialogResult 类型,则创建您自己的 DialogResult 类型并在 WPF DialogResult 和您的 DialogResult 之间提供代码 map(例如转换)。

You can have a look at IDialogService from In The Box MVVM Training - even though it's for WPF+MVVM (BTW: MVVM is a great pattern, really worth learning), it already includes everything you need.您可以查看In The Box MVVM Training中的 IDialogService - 尽管它适用于 WPF+MVVM(顺便说一句:MVVM 是一个很棒的模式,真的值得学习),它已经包含了您需要的一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM