异常/ MessageBox在Calibur.Micro [英] Exception/MessageBox in Calibur.Micro

查看:265
本文介绍了异常/ MessageBox在Calibur.Micro的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现一些博客,例如:



http://frankmao.com/2010/11/18/handling-messagebox-in-caliburn-micro/



例如查看一些方法模型类可以产生异常。

  public void MethodWichCanProduceEx(string arg1,string arg2)
{
if(arg1 == null)
throw new ArgumentNullException(arg1 is null);
if(arg2 == null)
throw new ArgumentNullException(arg2 is null);

try
{

}
catch(异常异常)
{

抛出异常;
//?显示消息框MessageBox.Shox(exception.Message)
}
}

什么是正确的处理和显示这些异常视图?它存在任何种类的caliburn.micro模式?



可能的跟踪异常如.NET中的文本,xml文件?



例如我想在xml,文本文件中的跟踪异常,并且在视图中只显示message.box或某些消息。



感谢提前,也许是我的问题很愚蠢,对不起,我只是在学习calibur.micro。

解决方案

你会一直在反对你的查看模型,在消息框的情况下,您不想在单元测试视图模型时等待用户输入。



您链接的Frank Mao代码使用代理从视图模型中抽取消息框的实现,但我将在此使用一个接口。您可以将代理视为具有单一方法的接口,但在此上下文中使用接口的优点在于您可以根据要显示的消息类型具有不同的方法。例如,您可以有一个ShowMessageError,ShowMessageWarning,ShowMessageInfo等。



因此,为您的消息框定义一个合同:

  public interface IMessageBox 
{
void ShowException(Exception exc);
}

将消息框依赖项注入到您的视图模型中,例如通过构造函数

  public class MyViewModel 
{
private readonly IMessageBox messageBox;

public MyViewModel(IMessageBox messageBox)
{
this.messageBox = messageBox;
}

public void MethodThatCanThrowException()
{
try {}
catch(异常exc)
{
//在这里记录异常
...
//显示消息框
this.messageBox.ShowException(exc);
}
}
}

然后可以实现消息无论如何,使用Windows系统消息框,或者更好的仍然使用您自己的视图/ viewmodel来显示消息,也许使用Caliburn.Micro WindowManager.ShowDialog()。



使用Windows系统消息框的实现可能如下所示:

  public class StandardMessageBox:IMessageBox 
{
public void ShowException(异常异常)
{
MessageBox.Show(exception.ToString(),Error Occurred);
}
}

在生产代码中,您可以针对IMessageBox注册StandardMessageBox在您的IoC容器中的界面。



在单元测试用地中,您可以嘲笑IMessageBox,并且不做任何操作,或者在消息框结果的方法的情况下,总是返回一个你想要的值。



为了记录异常,我将看一个日志框架,如log4net( http://logging.apache.org/log4net/index.html )或NLog( http://nlog-project.org/


I start learning Caliburn.Micro and I am little confuse of handling with exception/messange box in view model class.

I found some blogs about, for example:

http://frankmao.com/2010/11/18/handling-messagebox-in-caliburn-micro/

For example some method in view model class which can produce exception.

    public void MethodWichCanProduceEx(string arg1, string arg2 )
    {
        if(arg1==null)
            throw new ArgumentNullException("arg1 is null");
        if (arg2 == null)
            throw new ArgumentNullException("arg2 is null");

        try
        {

        }
        catch (Exception exception)
        {

            throw exception;
            //? show message box MessageBox.Shox(exception.Message)
        }
    }

What is correct handling and showing these exception in view ? It exist any kind of pattern for caliburn.micro?

It possible trace exception as in .NET in text, xml file ?

For example I would like trace exception in xml, text file and in view show only message.box or something message.

Thank for advance, maybe is my question little stupid, sorry I am only learning calibur.micro.

解决方案

You'll want to always work against abstractions in your view models, in the case of message boxes, you don't want to have to wait for user input when you come to unit test your view models.

The Frank Mao code you linked to uses a delegate to abstract the implementation of the message box from the view model, but I would use an interface here. You can think of a delegate as an interface with a single method, but the advantage of using an interface in this context is that you can have different methods depending on the type of message you wish to show. For example, you could have a ShowMessageError, ShowMessageWarning, ShowMessageInfo etc.

So, define a contract for your message box:

public interface IMessageBox
{
  void ShowException(Exception exc);
}

Inject the message box dependency into your view model, e.g. via the constructor

public class MyViewModel
{
  private readonly IMessageBox messageBox;

  public MyViewModel(IMessageBox messageBox)
  {
    this.messageBox = messageBox;
  }

  public void MethodThatCanThrowException()
  {
    try {}
    catch(Exception exc)
    {
      // log the exception here
      ...
      // show message box
      this.messageBox.ShowException(exc);
    }
  }
}

You can then implement the message box anyway you wish, either using the windows system message box, or nicer still use your own view/viewmodel to display the message, perhaps using the Caliburn.Micro WindowManager.ShowDialog().

An implementation that uses the windows system message box may look like:

public class StandardMessageBox : IMessageBox
{
  public void ShowException(Exception exception)
  {
    MessageBox.Show(exception.ToString(), "Error Occurred");
  }
}

In production code, you can register StandardMessageBox against the IMessageBox interface in your IoC container.

In unit test land, you can mock out IMessageBox and have it do nothing, or in the case of methods with a result from the message box, always return a value you wish.

For logging the exception, I would look at a logging framework such as log4net (http://logging.apache.org/log4net/index.html) or NLog (http://nlog-project.org/)

这篇关于异常/ MessageBox在Calibur.Micro的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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