Caliburn.Micro:在IResult从异常恢复 [英] Caliburn.Micro: Recovering from Exception in IResult

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

问题描述

这张贴在 Caliburn.Micro讨论也。我期待的意见和解决的最佳办法的意见。

This was posted at Caliburn.Micro discussions also. I'm really looking for advice and opinions on the best way to solve.

说我有以下操作

public IEnumerable<IResult> SaveStation()
{ 
    yield return Busy.MakeBusy();
    yield return new StationSave(_station);
    yield return Busy.MakeNotBusy();
    yield return Show.Tab<StationBrowseViewModel>();
}



StationSave是围绕一个简单的(WCF)服务调用的IResult包装。该服务使用FaultContract / FaultException异常的故障。

StationSave is an IResult wrapper around a simple (WCF) service invocation. The service uses FaultContract/FaultException for failures.

在出现故障的情况下,用户需要通知和FaultContract将包含有关什么地方出了错一些有用的信息。目前保存结果捕获异常并将其插入到已完成事件的ResultCompletionEventArgs。这样,由管道创建的SequentialResult被取消(因为错误的),从而留下处于繁忙状态屏幕。

In the case of a fault, the user needs to be notified and the FaultContract will contain some useful info about what went wrong. Currently the Save result catches the exception and inserts it into ResultCompletionEventArgs of the Completed event. By doing so, the SequentialResult created by the pipeline is cancelled (because of the error), thus leaving the screen in a Busy state.

我真正之后就是即将从错误中恢复(除去繁忙状态),并通知用户的最佳方式的想法(我有不同风格的通知,我想用一对夫妇IResult实现)的故障合同规定的细节。通过连接到我的虚拟机已完成的事件,我可以得到的错误,但在这一点上,我不再是行动管道的背景下,因此任何IResults我想用(在MakeNotBusy和我的节目通知的实现)我要手动执行(和我有新的了我自己的ActionExecutionContext,我不想做)。

What I'm really after is ideas about the best way to recover from the error (remove the busy state) and notify the user (I have a couple of IResult implementations for different styles of notification which I would like to use) of the details provided in the fault contract. By attaching to the Completed event in my VM I can get the error, but at this point I am no longer in the context of the Action pipeline so any IResults I would like to use (the MakeNotBusy and my show notification implementation) I have to execute manually (and I'd have to new up my own ActionExecutionContext which I don't want to be doing).

我有看马可阿门多拉的救助过滤器对于Caliburn.Micro从这里,但我再不能从救援方法传回IResults。

I have had a look at Marco Amendola's rescue filter for Caliburn.Micro from here, but again I can't pass back IResults from the Rescue method.

我错过了一些东西明显?如何处理别人这种情况?

Have I missed something obvious? How do others handle this situation?

推荐答案

两者的罗布艾森伯格马尔科·阿门多拉已在提供可能的解决方案<一个HREF =http://caliburnmicro.codeplex.com/Thread/View.aspx?ThreadId=244394相对=nofollow> CodePlex网站论坛。

Both Rob Eisenberg and Marco Amendola have provided possible solutions in the CodePlex forum.

我选择采取Marco的 RescueAttribute 从他的过滤器实现并修改它稍微允许救援方法的进一步 IResult 执行。这是需要改变 RescueAttribute.HandleException

I have chosen to take Marco's RescueAttribute from his filters implementation and modify it slightly to allow execution of further IResult from the rescue method. This is the required change to RescueAttribute.HandleException

protected override bool HandleException(ActionExecutionContext context,
                                        Exception ex)
{
    var method = context.Target
                        .GetType()
                        .GetMethod(MethodName, new[] { typeof(Exception) });
    if (method == null) return false;

    try
    {
        var result = method.Invoke(context.Target, new object[] { ex });

        if (result is bool)
            return (bool) result;

        if (result is IResult)
            result = new[] { result as IResult };
        if (result is IEnumerable<IResult>)
            Coroutine.Execute(((IEnumerable<IResult>) result).GetEnumerator(), context);
        else if (result is IEnumerator<IResult>)
            Coroutine.Execute(((IEnumerator<IResult>) result), context);

        return true;
    }
    catch
    {
        return false;
    }
}

这让我在VM以下内容:

this allows the following in my VM:

public IEnumerable<IResult> Rescue(Exception ex)
{
    yield return Busy.MakeNotBusy();
    // in practice pass exception details through to notification
    yield return new NotificationPopup("Save station failed");
}

[Rescue]
public IEnumerable<IResult> SaveStation()
{ 
    yield return Busy.MakeBusy();
    yield return new StationSave(_station);
    yield return Busy.MakeNotBusy();
    yield return Show.Tab<StationBrowseViewModel>();
}

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

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