简体   繁体   中英

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:

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:

  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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