2

我试图了解何时以及为什么使用 Notification 类。我看到它与 ToNotifier 和 FromNotifier 有关,但不能完全确定它们的用途。

它是为了在可观察序列和观察者之间创建一种类似绑定的两种方式,因此观察者可以通过通知将更改推送回原始可观察对象,还是?

谢谢,埃吉尔。

4

3 回答 3

2

The only time I've ever personally used Notification<T> was when I had to do some freaky error trapping across multiple merged streams...here's a trimmed down semi-unrealistic example of what I was doing:

public class ImportantException : Exception {}

var src1 = new Subject<int>();
var src2 = new Subject<int>();  
var wrapped = Observable.Merge(src1.Materialize(), src2.Materialize());

var query = 
    from note in wrapped
    let fail = note.Kind == NotificationKind.OnError
    let ignorable = fail && !(note.Exception is ImportantException)
    where !fail || !ignorable
    select note;

using(query.Subscribe(Console.WriteLine))
{
    src1.OnNext(1);
    src2.OnNext(1);
    src1.OnError(new Exception());
    src2.OnError(new ImportantException());
}

So basically, I only wanted certain types of errors from multiple (in my case, dozens) of merged streams to percolate out to the subscriber. Here's the output of the above unrealistic example:

OnNextNotification<int> { Value = 1 }
OnNextNotification<int> { Value = 1 }
OnErrorNotification<int> { Exception = (ImportantException) }

(and yes, you can replicate this with some tinkering, using Catch constructs, but I found it easier to read-through in this form)

于 2013-04-29T15:32:41.633 回答
2

我在这里写了一些关于物化和通知的文章:

http://www.zerobugbuild.com/?p=47

并在这里使用它:

http://social.msdn.microsoft.com/Forums/en-US/rx/thread/bbcc1af9-64b4-456b-9038-a540cb5f5de5

于 2013-05-09T22:04:06.863 回答
1

我最近在尝试对我的应用程序服务中的 Rx 查询进行单元测试时开始使用它。我现在可以将 a 传递TestScheduler给正在测试的服务调用,让 Rx 查询在 上生成事件TestScheduler,然后单元测试可以访问这些事件以作为Notification<>实例集合进行检查。您可以在本文测试 Rx 查询部分中看到该方法。

于 2013-04-28T13:48:54.403 回答